[英]How to check value null and replace it in NSDictionary
现在,我正在使用NSUserDefault和NSDictionary,我将NSDictionary保存在NSUserDefault中,不幸的是我不能,因为NSDictionary返回Json的值为空。
我需要检查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>"
}
]
})
能否请您检查此链接,我想,它将对您有所帮助,感谢此链接替换嵌套NSDictionary中出现的NSNull
UPDATE
:我对原版进行了一些修改,并使用了一些从此链接将nsarray转换为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 = "";
};
}
希望它能对您有所帮助:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.