iphone - 类实例中未调用委托(delegate)方法

标签 iphone delegates amazon-s3 subclass

我正在创建一个名为 S3ObjectController (S3OC) 的类的实例,该类具有一个方法和四个委托(delegate)方法。我创建了 S3OC 的实例,从 S3OC 类调用实例方法(我知道该方法是从 NSLog 语句触发的),但在 S3OC 类中没有调用任何关联的委托(delegate)方法。我在方法中将委托(delegate)设置为 self,并在 .h header 中正确声明了委托(delegate)。

想法?需要明确的是,我认为应该调用下面的 .m 文件中的 (void)request 方法,但事实并非如此。我收到 EXC BAD ACCESS 错误。 self 是否被 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");
}

最佳答案

我认为您对 ARC 的怀疑是正确的。由于委托(delegate)属性通常是弱引用,因此它们不足以阻止对象被释放。

创建一个作为 iVar 的 NSArray 并将 S3ObjectController 添加到其中。如果委托(delegate)们仍然不开火,你就知道是别的事情了......

编辑:

因此,在包含 for 循环的类的 header 中声明一个 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");
     }
}

关于iphone - 类实例中未调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8974720/

相关文章:

php - 在 iPhone 中使用多部分表单数据发布图像和其他数据

ios - iAd Banner 未显示在已批准的 iPhone 应用程序中

c# - c# 中委托(delegate)的输入和输出是什么?

c# - 为什么我不能使用这样的匿名方法?

ios - 子类化 NSInputStream,重写委托(delegate)?

powershell - 为什么 Set-SQSQueueAttribute 失败并显示 'InvalidOperationException: Invalid value for the parameter Policy' 错误?

python - 这个python函数如何获取它的参数?

iphone - 如何将 shapefile 转换为基于图 block 的文件

iphone - AudioQueue 录制音频样本

hadoop - 执行S3NativeFileSystem.mkdir时出现AccessDeniedException