我无法将用于云数据库服务的静态库移植到ARC。
我已经到了编译和运行的阶段,但是它从来没有回调过委托。
我有2个类,一个代理类和一个APIOperation。
APIOperation是NSOperation的子类,它使用NSURLConnection从Web API中获取数据。
代理具有NSOperationQueue,并且基本上是所有APIOperation调用的委托。
使用模型如下:
用户类创建代理对象的实例。
代理实例化APIOperation对象并将其添加到NSOperationQueue
APIOperation创建NSURLConnection
在connectionDidFinishLoading
解析响应,然后通过NSInvocation传递回代理类。
代理类调用委托(用户类)并传回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 responsesToSelector ...')行为止,这时它总是返回false。现在假设这与ARC有关,我已经检查了委托是否为null并且该值在那里,APIOperation.h中的委托属性也声明为:
@property (unsafe_unretained) id<KSAPIOperationDelegate,NSObject> delegate;
如果我删除了reactsToSelector检查,则应用程序在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 responseToSelector在NSOperation中始终返回false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8944501/