Delegate Methods not called in Class Instance
我正在创建一个名为S3ObjectController(S3OC)的类的实例,该类有一个方法和四个委托方法。我创建了一个s3oc实例,从s3oc类调用一个实例方法(我知道它是从nslog语句激发的),但是在s3oc类中没有调用任何相关的委托方法。我在方法中将委托设置为self,并在.h头中正确声明委托。
思想?为了清楚起见,我认为下面的.m文件中的(void)请求方法应该被调用,而不是这样的。我正遇到异常严重的访问错误。ARC是否释放了自我?
s3oc类的整个.m文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #import"S3ObjectController.h" @implementation S3ObjectController @synthesize string; @synthesize s3GOR, s3Client; -(void)method { NSLog(@"Method Called"); s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY]; s3GOR = [[S3GetObjectRequest alloc]initWithKey:string withBucket:[Constants pictureBucket]]; [s3GOR setDelegate:self]; [s3Client getObject:s3GOR]; NSLog(@"Method Finished"); } -(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error { NSLog(@"Error %@",error); } -(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response { NSLog(@"Response Key %@", response); } -(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data { NSLog(@"ObjectRequestKey = %@",request); } -(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response { NSLog(@"Final Delegate Method"); } |
这是标题:"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @interface S3ObjectController : NSObject <AmazonServiceRequestDelegate>{ NSMutableData *responseData; NSString *string; AmazonS3Client *s3Client; S3GetObjectRequest *s3GOR; } -(void)method; @property (nonatomic, strong) NSString *string; @property (nonatomic, strong) S3GetObjectRequest *s3GOR; @property (nonatomic, strong) AmazonS3Client *s3Client; @end |
最后,下面是我如何在另一个类中调用该方法:
1 2 3 4 5 6 7 | for (NSString *name in nameArray){ @try { S3ObjectController *localS3 = [[S3ObjectController alloc]init]; localS3.string = name; [localS3 method]; NSLog(@"called"); } |
我认为你对ARC的怀疑是真的。因为委托属性通常是弱引用,所以它们不足以阻止对象被释放。
制作一个作为IVAR的NSarray,并将S3ObjectController添加到其中。如果代表们仍然不开火,你知道这是另外一回事…
编辑:
因此,在包含for循环的类的头中声明一个nsmutablerray,在如下位置初始化它:
1 | myArray = [NSMutableArray arrayWithCapacity:0]; |
然后这样使用:
1 2 3 4 5 6 7 8 9 | for (NSString *name in nameArray){ @try { S3ObjectController *localS3 = [[S3ObjectController alloc]init]; localS3.string = name; [localS3 method]; [myArray addObject:localS3]; NSLog(@"called"); } } |