Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

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

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

I get a very strange error with this code:

- (NSMutableURLRequest*)setupRequestWithService:(NSString*)service andMethod:(NSString*)method
{
    NSString* url = [NSString stringWithFormat:@"%@%@.svc/%@", serverUrl, service, method];

    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

    // Set authentication token.
    NSLog(@"???????????? %@", authenticationToken);
    if (authenticationToken == nil)
        NSLog(@"NULL AUTHTOKEN");
    if ([authenticationToken isEqual:[NSNull null]])
        NSLog(@"NSNULL AUTHTOKEN");
    if (request == nil)
        NSLog(@"NULL REQUEST");
    [request addValue:authenticationToken forHTTPHeaderField:REQUEST_HEADER_AUTH_TOKEN];

    return request;
}

This is my log:

???????????? <null>
NSNULL AUTHTOKEN
-[NSNull length]: unrecognized selector sent to instance 0x3b5a5090
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x3b5a5090'

It seems that authenticationToken is NULL. But I don't understand that, if authenticationToken is NULL why I don't see NULL AUTHTOKEN on the log.

I get this error the second time I run that method, the first time, I don't get any error. This is my log:

???????????? (null)
NULL AUTHTOKEN

By the way:

NSString* authenticationToken;

Any advice?

Maybe there is a Memory Leak somewhere...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...