I am getting an array with null value. Please check the structure of my array below:
(
\"< null>\"
)
When I\'m trying to access
I found the code for working with NSNull
has the following problems:
So I created the following category:
@interface NSObject (NSNullUnwrapping)
/**
* Unwraps NSNull to nil, if the object is NSNull, otherwise returns the object.
*/
- (id)zz_valueOrNil;
@end
With the implementation:
@implementation NSObject (NSNullUnwrapping)
- (id)zz_valueOrNil
{
return self;
}
@end
@implementation NSNull (NSNullUnwrapping)
- (id)zz_valueOrNil
{
return nil;
}
@end
It works by the following rules:
Class
(ie singleton instance of the Class
type) then behavior is undefined. However, a method declared in a subclass is allowed to override a category method in its super-class. This allows for more terse code:
[site setValue:[resultSet[@"main_contact"] zz_valueOrNil] forKey:@"mainContact"];
. . as opposed to having extra lines to check for NSNull
. The zz_
prefix looks a little ugly but is there for safety to avoid namespace collisions.