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

前端 未结 4 1040
悲哀的现实
悲哀的现实 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 03:42

    The problem comes because your method return an NSNull object. You can't check [authenticationToken isEqual:[NSNull null]]) because [NSNull null] give an instance of an object. So it's different from your object itself. If you want to check if you received an NSNull object you need to check like this: [authenticationToken isKindOfClass:[NSNull class]] instead.

    0 讨论(0)
  • 2020-12-01 03:50

    The error message is pretty clear. NSNull and nil are different things:

    The NSNull class defines a singleton object used to represent null values in 
    collection objects (which don’t allow nil values).
    

    If you want to check if authenticationToken is NSNull try: [authenticationToken isEqual: [NSNull null]]

    0 讨论(0)
  • 2020-12-01 03:54

    My solution to this maddening use of NSNull by JSON interpreters is to create a category on NSNull, where I define integerValue, floatValue, length, etc - return 0 for all. Everytime you get another crash add a new category. I think I had 6 or 7 when I had this issue.

    The problem with NOT doing this is you have to look for the NULL everywhere in your converted objects - a PITA in my opinion.

    EDIT: the code I'm using, all in a NSNull+JSON.m file:

    @interface NSNull (JSON)
    @end
    
    @implementation NSNull (JSON)
    
    - (NSUInteger)length { return 0; }
    
    - (NSInteger)integerValue { return 0; };
    
    - (float)floatValue { return 0; };
    
    - (NSString *)description { return @"0(NSNull)"; }
    
    - (NSArray *)componentsSeparatedByString:(NSString *)separator { return @[]; }
    
    - (id)objectForKey:(id)key { return nil; }
    
    - (BOOL)boolValue { return NO; }
    
    @end
    

    EDIT2: Now in Swift 3:

    extension NSNull {
       func length() -> Int { return 0 }
    
       func integerValue() -> Int { return 0 }
    
       func floatValue() -> Float { return 0 };
    
       open override var description: String { return "0(NSNull)" }
    
       func componentsSeparatedByString(separator: String) -> [AnyObject] { return [AnyObject]() }
    
       func objectForKey(key: AnyObject) -> AnyObject? { return nil }
    
       func boolValue() -> Bool { return false }
    }
    
    0 讨论(0)
  • 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:.

    0 讨论(0)
提交回复
热议问题