返回空值「
我做一個POST請求到服務器,並將其與"someVal" = "<null>"
,而不是"someVal" = null
響應。這使得Xcode中對付它在一個非常奇怪的方式,我似乎無法保存,因爲它... NSUserDefaults的,我不能刪除它就像我一個正常的空值...那會被檢測爲?我想也許是一個字符串?但是,不能......因爲某種原因,它不能保存爲一個字符串。
返回空值「
我做一個POST請求到服務器,並將其與"someVal" = "<null>"
,而不是"someVal" = null
響應。這使得Xcode中對付它在一個非常奇怪的方式,我似乎無法保存,因爲它... NSUserDefaults的,我不能刪除它就像我一個正常的空值...那會被檢測爲?我想也許是一個字符串?但是,不能......因爲某種原因,它不能保存爲一個字符串。
我不認爲這個問題是有限的,只是RestKit。更多來自爲您提供JSON的服務器。我在使用AFNetworking時遇到了同樣的問題。
我發現這個字典類別,你可以用它來刪除空值並用空字符串替換。你可以使用它,或者只是將邏輯翻錄出來,以排序你的空值。
@implementation NSDictionary (JRAdditions)
- (NSDictionary *) dictionaryByReplacingNullsWithStrings {
NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:self];
const id nul = [NSNull null];
const NSString *blank = @"";
for(NSString *key in self) {
const id object = [self objectForKey:key];
if(object == nul) {
//pointer comparison is way faster than -isKindOfClass:
//since [NSNull null] is a singleton, they'll all point to the same
//location in memory.
[replaced setObject:blank forKey:key];
}
}
return [NSDictionary dictionaryWithDictionary:replaced];
}
+ (NSDictionary *)dictionaryByReplacingNullsWithStrings:(NSDictionary *)dict {
NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:dict];
const id nul = [NSNull null];
const NSString *blank = @"";
for(NSString *key in dict) {
const id object = [dict objectForKey:key];
if(object == nul) {
//pointer comparison is way faster than -isKindOfClass:
//since [NSNull null] is a singleton, they'll all point to the same
//location in memory.
[replaced setObject:blank forKey:key];
}
}
return [NSDictionary dictionaryWithDictionary:replaced];
}
@end
// you can use this category on your dictionaries.
NSDictionary *jsonDict = JSON;
jsondict = [jsonDict dictionaryByReplacingNullsWithStrings];
您確定該值存儲爲字符串「」「」嗎?因爲印刷字典的描述與'[NSNull空]'值也產生輸出'鍵=「」'。 –