gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:04:12 26 4
gpt4 key购买 nike

我正在创建一个名为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com