-[Nsnull Length]: Unrecognized Selector Sent to JSON Objects

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

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

[NSNull length]: unrecognized selector sent to instance

titleForRow: return type in NSString so you can not return 0. Yopu have to return return nil or return @""

[NSNull length]: unrecognized selector sent to instance 0x43fe068

It is hard, according to what you posted, say what exactly is wrong except the obvious facts. The good start point might be adding a category on NSNull implementing length method and set there a break point and see when it gets called (probably you will need to import that category in a .pch file)

[NSNull length]: unrecognized selector sent to instance in objective-c

nil is returned by the dictionary if no object with that key was found.

An instance of NSNull is inserted by the JSON parser to indicate that a null-valued key was present.

You need to check whether the object you got back was really a string. You are failing to catch the case where there is an object but it is not a string.

E.g.

if ([albm.thumbnail isKindOfClass:[NSString class]])
... a string-form URL is present ...
else
.... something else, or nothing, was found; use a fallback ...

NSInvalidArgumentException -[NSNull length]: unrecognized selector sent to instance

[NSNull null] is a special singleton value used to represent null values in collection types that don't allow nil such as NSArray and NSDictionary.

If you know that a certain value, coming from a server for example, can be null you need to check for that before attempting to treat it as some other value

NSDictionary *data = ...
NSString *name = [data objectForKey:@"name"];
// This will cause a crash if the value of the
// `name` key was `null` since objectForKey:
// will return [NSNull null]
if ([name length] > 0) {
// Do something
}

What you want to do is check that the value is not null before using it in any way

if (name != [NSNull null]) {
if ([name length] > 0) {
// Do something
}
}

Only iOS 7 crash [NSNull intValue]: unrecognized selector sent to instance

Put a check before accessing the value from JSON like,

if([NSNull null] != [listDic objectForKey:@"voteCount"]) {
NSString *voteCount = [listDic objectForKey:@"voteCount"];
/// ....
}

Reason for checking is, collection objects like NSDictionary do not allow values to be nil, hence they are stored as null. Passing intValue to a NSNull will not work as it will not recognise this selector.

Hope that helps!



Related Topics



Leave a reply



Submit