문제

PLEASE SEE EDIT #1 AT BOTTOM FOR WHAT SEEMS TO FIX IT

I have a JSON payload being returned that could either be null or value. Like:

post: {
   header:"here is my header",
   current_instore_anncouncement: null
}

or when there is a current_instore_announcement:

post: {
   header:"here is my header",
   current_instore_anncouncement: {header: "announcement header"}
}

I am using AFNetworking 2.0 and want to either show the current_isntore_announcement if it exists or do nothing if it returns null.

How would I properly check for the existence of the current_instore_anncouncement? I have:

NSDictionary *current_instore_announcement=[result objectForKey:@"current_instore_anncouncement"];

if(current_instore_announcement){
  InstoreAnnoucement *splashAnnouncement = [[InstoreAnnoucement alloc] initWithAttributes:current_instore_announcement];
if(splashAnnouncement){
    NSLog(@"theres an instore announcement");
}else{
    NSLog(@"nadda = instore announcement with %@",current_instore_announcement);
}
}

but I get the following error:

[NSNull notNullObjectForKey:]: unrecognized selector sent to instance 

thx for any help

edit #1 It looks like the best way is to check for it not being NSNull. Like so:

if(![current_instore_announcement isEqual:[NSNull class]]){

or to create an addition to NSDictionary like this:

#import "NSDictionary+Additions.h"

@implementation NSDictionary (Additions)

- (id)notNullObjectForKey:(id)aKey {
    id obj = [self objectForKey:aKey];
    if ([obj isKindOfClass:[NSNull class]]) {
        return nil;
    }
    
    return obj;
}

Any other / better ideas?

도움이 되었습니까?

해결책

i use following methods:

if(![current_instore_announcement isEqual:[NSNull class]]){
    // Not null
}
else{
    // null
}

다른 팁

The good way to check this is by following method. please check this:

- (BOOL) hasValue:(id)object {

    if(object!=nil && (NSNull *)object != [NSNull null])    {

    // Check NSString Class
    if([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSMutableString class]]) {
        if([object length]>0) {
            return YES;
        }
     } 

    // Check UIImage Class
    if([object isKindOfClass:[UIImage class]]){
        if ([object CGImage]!=nil) {
            return YES;
        }
     }

    // Check NSArray Class
    if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSMutableArray class]]) {
        if ([object count] > 0) {
            return YES;
        }
     }
    else {
        return YES;
     }
  }
  return NO;
 }

and than you have to check every time for every object from your json response.

You can check by use of [myDisctionary allKeys];

Ex:

NSDictionary *current_instore_announcement=[result objectForKey:@"current_instore_anncouncement"];

// get the keys
NSArray *keys = [current_instore_announcement allKeys];

if([keys count]>0){
    // do something with the dictionary.
}else
    // Null Values
NSDictionary *current_instore_announcement=[result objectForKey:@"current_instore_anncouncement"];
if ([current_instore_announcement isKindOfClass:[NSDictionary class]]){
    //nil or null will not be here, and just do NSDictionary parsing here
}

or else:

if (![current_instore_announcement isKindOfClass:[NSNull class]]){
    //not null class will be here, NSDictionary, nil, empty string will all be parsed here
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top