CopyPastor

Detecting plagiarism made easy.

Score: 1.9227465226815383; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2022-08-30
by Mansoor Malik

Original Post

Original - Posted on 2013-05-17
by David H



            
Present in both answers; Present only in the new answer; Present only in the old answer;

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



        
Present in both answers; Present only in the new answer; Present only in the old answer;