gpt4 book ai didi

iphone - iOS 5.0 respondsToSelector 在 NSOperation 中总是返回 false

转载 作者:行者123 更新时间:2023-11-28 17:38:50 25 4
gpt4 key购买 nike

我无法将我们的云数据库服务的静态库移植到 ARC。

我已经到了编译和运行的阶段,但它永远不会回调委托(delegate)。

我有 2 个类,一个代理类和一个 APIOperation。

APIOperation 是 NSOperation 的子类,它使用 NSURLConnection 从 Web API 获取数据。

代理有一个 NSOperationQueue,基本上是所有 APIOperation 调用的委托(delegate)。

使用模型如下:

  • 用户类创建代理对象的实例。
  • Proxy 实例化 APIOperation 对象并将其添加到 NSOperationQueue
  • APIOperation 创建 NSURLConnection
  • 在 connectionDidFinishLoading
    • 解析响应然后通过 NSInvocation 传回代理类。
    • 代理类调用委托(delegate)(用户类)并传回 API 响应。

代码如下:

代理类:

@implementation theProxy

@synthesize callbackSelector,delegate,opQueue;

-(theProxy*)init{
opQueue = [[NSOperationQueue alloc]init];
return self;
}

- (void) apiOperation:(APIOperation*)operation didCompleteWithResult:(NSArray*)theResult{
SEL selector = [operation callbackSelector];
if ([delegate respondsToSelector:selector]) {
NSInvocation* inv = [NSInvocation invocationWithMethodSignature:[[delegate class] instanceMethodSignatureForSelector:selector]];
[inv setTarget:delegate];
[inv setSelector:selector];
theProxy* tmp = self;
[inv setArgument:&tmp atIndex:2];
[inv setArgument:&operation atIndex:3];
[inv setArgument:&theResult atIndex:4];
[inv invoke];
}

}

- (void) apiOperation:(APIOperation*)operation didFailWithError:(NSString*)theError{
if ([delegate respondsToSelector:@selector(API:apiOperation:didFailWithError:)]) {
[delegate API:self apiOperation:operation didFailWithError:theError];
}
}

-(void)cancelAllOperations{
[opQueue cancelAllOperations];
}

- (void)dealloc
{
[opQueue cancelAllOperations];
[opQueue release], opQueue = nil;
delegate = nil;
//[delegate release]; delegate should not be retained.

[super dealloc];
}

APIOperation 类(大大简化):

@implementation APIOperation
@synthesize delegate,APIKey,secretKey,debugMode,callbackSelector,successCallbackMethodSignature,errorCallbackMethodSignature,delegateCallbackMethodSignature,tag,APIServer,requestProcessingTime,requestReceivedTime,responseCode,responseMessage,timestamp,requestRoundTripTime,requestStartMicroTime,useSSL;

-(void) main{
receivedData = [NSMutableData data];
connFinished = NO;
// create the connection with the request
// and start loading the data
theConnection=[[NSURLConnection alloc] initWithRequest:[self buildRequest] delegate:self];
if (theConnection) {

do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!connFinished);
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

id pList = [NSPropertyListSerialization propertyListFromData:receivedData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&errorStr];

theResponse = (NSDictionary*) pList;

if ([delegate respondsToSelector:@selector(apiOperation: didCompleteWithResult:)]) {

NSArray* theResultsArray = [theResponse objectForKey:@"payload"];

NSInvocation *inv = [NSInvocation invocationWithMethodSignature:successCallbackMethodSignature];
[inv setTarget:delegate];
[inv setSelector:@selector(apiOperation: didCompleteWithResult:)];
KSAPIOperation* tmp = self;
[inv setArgument:&tmp atIndex:2];
[inv setArgument:&theResultsArray atIndex:3];
[inv performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES];

}
}
@end

现在正如我所说,这一直有效到 connectionDidfinishLoading 中的“if([delegate respondsToSelector...”行。那时它总是返回 false。现在假设这与 ARC 有关,我已经检查了委托(delegate)不为空并且值在那里,委托(delegate)属性也在 APIOperation.h 中声明为:

@property (unsafe_unretained) id<KSAPIOperationDelegate,NSObject> delegate;

如果我删除 respondsToSelector 检查,则应用程序会在 main() 中崩溃,并显示以下回溯:

#0  0x0156b09b in objc_msgSend ()
#1 0xbfffde10 in ?? ()
#2 0x0132d437 in -[NSInvocation invoke] ()
#3 0x013c8e72 in -[NSObject performSelector:withObject:] ()
#4 0x009369ef in __NSThreadPerformPerform ()
#5 0x0139b97f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#6 0x012feb73 in __CFRunLoopDoSources0 ()
#7 0x012fe454 in __CFRunLoopRun ()
#8 0x012fddb4 in CFRunLoopRunSpecific ()
#9 0x012fdccb in CFRunLoopRunInMode ()
#10 0x012b0879 in GSEventRunModal ()
#11 0x012b093e in GSEventRun ()
#12 0x0001ea9b in UIApplicationMain ()
#13 0x00002a58 in main (argc=1, argv=0xbfffed50) at /Users/MikeW/Desktop/ARC test proj/lib1.0Test/lib1/main.m:16
#14 0x000029b5 in start ()

非常感谢您提供的任何帮助。

谢谢

迈克

最佳答案

这与 ARC 没有任何关系。

如果它报告它没有响应选择器,那么它没有。弄清楚。您很可能在选择器中输入了错误(例如,它们区分大小写)。或者代理实际上不是您认为的那样。

顺便说一句,我会从您的选择器中删除空格,例如@selector(apiOperation: didCompleteWithResult:) 看起来是错误的,即使编译器喜欢它也是如此。

关于iphone - iOS 5.0 respondsToSelector 在 NSOperation 中总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8944501/

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