-[NSNull length]: unrecognized selector sent to JSON objects

前端 未结 4 1043
悲哀的现实
悲哀的现实 2020-12-01 03:00

I\'m developing an iOS 5.0+ app with latest SDK.

I get a very strange error with this code:

- (NSMutableURLRequest*)setupRequestWithService:(NSString         


        
4条回答
  •  自闭症患者
    2020-12-01 04:00

    In line with David H's answer, how about a category on NSNull that just uses ObjC's message forwarding to "do nothing", to emulate the runtime's behavior when sending messages to nil?

    Like this:

    @interface NSNull (ForwardInvocation)
    
    @end
    
    @implementation NSNull (ForwardInvocation)
    
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
        return [NSNull methodSignatureForSelector:@selector(description)];
    }
    
    - (void)forwardInvocation:(NSInvocation *)anInvocation {
        // do nothing; prevent 'unrecognized selector' crashes
    }
    
    @end
    

    The [NSNull methodSignatureForSelector:@selector(description)]; takes advantage of the fact that NSNull inherits from NSObject, which provides the description method. This satisfies the forwarding mechanism requirement for implementing -methodSignatureForSelector:.

提交回复
热议问题