现在,我正在使用 NSUserDefault 和 NSDictionary,我将 NSDictionary 保存在 NSUserDefault 中,不幸的是我不能,因为返回 Json 的 NSDictionary 具有空值。
我需要检查 NSDictionary 是否有空值并替换。怎么办?
这是 NSDictionary,
({
ID:11,
name:21-12-2012,
des:"<null>",
url:
[
{
ID:1,
name: "<null>"
},
{
ID:2,
name:"<null>"
}
]
},
{
ID:12,
name:if i die young,
des:"<null>",
url:
[
{
ID:3,
name: "<null>"
},
{
ID:21,
name:"<null>"
}
]
})
最佳答案
能否请您查看此链接,我认为它会对您有所帮助,感谢您提供此链接 Replace occurrences of NSNull in nested NSDictionary
更新
:我稍微修改了原版,并使用了这个链接Convert NSArray to NSDictionary 中的一些将nsarray 转换为nsdictionary 的功能。 ,因为我对你的代码一无所知,所以我试着让我的 json 字符串尽可能接近你的,并且它的工作,请参见以下内容。 :)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tmpDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *testDict = [[NSMutableDictionary alloc] init];
[testDict setValue:[NSNull null] forKey:@"NullValue"];
[testDict setValue:@"test" forKey:@"UnNull"];
subArr = [[NSMutableArray alloc] initWithObjects:testDict, testDict, nil];
[tmpDict setValue:[NSNull null] forKey:@"NullHere"];
[tmpDict setValue:@"wear" forKey:@"NotNull"];
[tmpDict setObject:subArr forKey:@"Array"];
myArr = [[NSMutableArray alloc] initWithObjects:tmpDict, tmpDict, nil];
NSLog(@"struct: %@", myArr);
[self dictionaryByReplacingNullsWithStrings];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) dictionaryByReplacingNullsWithStrings {
const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: [self indexKeyedDictionaryFromArray:myArr]];
const id nul = [NSNull null];
const NSString *blank = @"";
for (NSString *key in [replaced allKeys]) {
const id object = [replaced objectForKey: key];
if (object == nul) {
[replaced setObject: blank forKey: key];
}
else if ([object isKindOfClass: [NSDictionary class]]) {
NSLog(@"found null inside and key is %@", key);
[replaced setObject:[self replaceNullInNested:object] forKey:key];
}
}
NSLog(@"replaced: %@", replaced);
}
- (NSMutableDictionary *)replaceNullInNested:(NSDictionary *)targetDict
{
//make it to be NSMutableDictionary in case that it is nsdictionary
NSMutableDictionary *m = [targetDict mutableCopy];
NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: m];
const id nul = [NSNull null];
const NSString *blank = @"";
for (NSString *key in [replaced allKeys]) {
const id object = [replaced objectForKey: key];
if (object == nul) {
[replaced setObject: blank forKey: key];
}
else if ([object isKindOfClass: [NSArray class]]) {
NSLog(@"found null inside and key is %@", key);
//make it to be able to set value by create a new one
NSMutableArray *a = [object mutableCopy];
for (int i =0; i< [a count]; i++) {
for (NSString *subKey in [[a objectAtIndex:i] allKeys]) {
// NSLog(@"key: %@", subKey);
// NSLog(@"value: %@", [[object objectAtIndex:i] valueForKey:subKey]);
if ([[object objectAtIndex:i] valueForKey:subKey] == nul) {
[[object objectAtIndex:i] setValue:blank forKey:subKey];
}
}
}
//replace the updated one with old one
[replaced setObject:a forKey:key];
}
}
return replaced;
}
- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
id objectInstance;
NSUInteger indexKey = 0;
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
for (objectInstance in array)
[mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]];
return (NSDictionary *)mutableDictionary;
}
从上面的代码,这是结果:
replacenullvalue[1590:11303] struct: (
{
Array = (
{
NullValue = "<null>";
UnNull = test;
},
{
NullValue = "<null>";
UnNull = test;
}
);
NotNull = wear;
NullHere = "<null>";
},
{
Array = (
{
NullValue = "<null>";
UnNull = test;
},
{
NullValue = "<null>";
UnNull = test;
}
);
NotNull = wear;
NullHere = "<null>";
}
)
2012-12-16 15:16:22.790 replacenullvalue[1590:11303] found null inside and key is 0
2012-12-16 15:16:22.790 replacenullvalue[1590:11303] found null inside and key is Array
2012-12-16 15:16:22.791 replacenullvalue[1590:11303] found null inside and key is 1
2012-12-16 15:16:22.791 replacenullvalue[1590:11303] found null inside and key is Array
2012-12-16 15:16:22.792 replacenullvalue[1590:11303] replaced: {
0 = {
Array = (
{
NullValue = "";
UnNull = test;
},
{
NullValue = "";
UnNull = test;
}
);
NotNull = wear;
NullHere = "";
};
1 = {
Array = (
{
NullValue = "";
UnNull = test;
},
{
NullValue = "";
UnNull = test;
}
);
NotNull = wear;
NullHere = "";
};
}
希望对您有所帮助:).
关于ios - 如何检查值 null 并在 NSDictionary 中替换它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13889285/