我正在创建一个名为S3ObjectController(S3OC)的类的实例,它有一个方法和四个委托方法。我创建了一个S3OC实例,从S3OC类调用一个实例方法(我知道从NSLog语句中触发)但是在S3OC类中没有调用任何关联的委托方法。我在方法中将委托设置为self,并在.h标头中正确声明委托。
思考?为了清楚起见,它是下面.m文件中的(void)请求方法,我认为应该调用它不是。我收到了EXC BAD ACCESS错误。 ARC自我释放了吗?
S3OC类的整个.m文件如下:
#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");
}
这是标题:“
@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
最后,这是我在另一个类中调用该方法的方法:
for (NSString *name in nameArray){
@try {
S3ObjectController *localS3 = [[S3ObjectController alloc]init];
localS3.string = name;
[localS3 method];
NSLog(@"called");
}
答案 0 :(得分:2)
我认为你对ARC的怀疑是真的。因为委托属性通常是弱引用,所以它们不足以阻止对象被释放。
创建一个iVar的NSArray并将S3ObjectController添加到它。如果代表们仍然不开火,你知道这是另一回事......
编辑:
所以在包含for循环的类的标题中声明一个NSMutableArray,在这样的地方初始化它:
myArray = [NSMutableArray arrayWithCapacity:0];
然后像这样使用它:
for (NSString *name in nameArray){
@try {
S3ObjectController *localS3 = [[S3ObjectController alloc]init];
localS3.string = name;
[localS3 method];
[myArray addObject:localS3];
NSLog(@"called");
}
}