我已經解決了這個問題,有一個簡單的這個簡單的Objective-C類:
的NSDictionary + NotNull.h
#import <Foundation/Foundation.h>
/*! This category extends NSDictionary to work around an issue with NSNull object.
*/
@interface NSDictionary (NotNull)
/*! @abstract Returns the value associated with a given key, but only if the value is not NSNull.
@param aKey The key for which to return the corresponding value.
@return The value associated with the given key, or nil if no value is associated with the key, or the value is NSNull.
*/
- (id)objectOrNilForKey:(id)aKey;
@end
的NSDictionary + NotNull.m
#import "NSDictionary+NotNull.h"
@implementation NSDictionary (NotNull)
- (id)objectOrNilForKey:(id)aKey
{
id object = [self objectForKey:aKey];
if (object == [NSNull null]) {
return nil;
}
return object;
}
@end
現在你可以撥打電話:
NSArray *galleryArr = [item objectOrNilForKey:@"photos"];
gallerAr r不是null ..你會得到像'[__dataType count]'無法識別的選擇器...總之'galleryArr'被聲明爲NSArray ..但實際上它不是 – Shubhank
這是來自JSON對象嗎?因爲你可能會得到'NSNull'的實例,它不等於'nil' –
它來自JSON對象,我試過 if(galleryArr!= [NSNull null]){ 沒有工作。 – Boaz