NSJSONSerialization没有创建可变容器 [英] NSJSONSerialization not creating mutable containers
问题描述
以下是代码:
NSError *parseError;
NSMutableArray *listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[]" dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&parseError];
NSLog(@"Is mutable? %li", [listOfObjects isKindOfClass:[NSMutableArray class]]);
listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[[],{}]" dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&parseError];
NSLog(@"Is mutable? %li", [listOfObjects isKindOfClass:[NSMutableArray class]]);
正如你所看到的,我正在调用完全相同的方法来解析JSON两次,一次使用JSON中的空列表,然后是包含内部对象的列表。结果如下:
As you can see, I'm calling exactly the same method for parsing the JSON both times, one with an empty list in the JSON, and then a list with an object inside. Here's the result:
Is mutable? 0
Is mutable? 1
问题是NSJSONSerialization似乎没有遵循为空创建可变容器的选项名单。对我来说似乎是一个错误,但也许我只是误解了一些事情。
The problem is that the NSJSONSerialization doesn't seem to follow the option to create mutable containers for empty lists. Seems like a bug to me, but maybe I just misunderstanding things.
任何想法?
推荐答案
这与预期一样:
NSString *s = @"{ \"objs\": [ \"a\", \"b\" ] }";
NSData *d = [NSData dataWithBytes:[s UTF8String] length:[s length]];
id dict = [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingMutableContainers error:NULL];
NSLog(@"%@", dict);
[[dict objectForKey:@"objs"] addObject:@"c"];
NSLog(@"%@", dict);
NSLog(@"%@", [[dict objectForKey:@"objs"] class]);
这是控制台输出:
2012-03-28 13:49:46.224 ExampleRunner[42526:707] {
objs = (
a,
b
);
}
2012-03-28 13:49:46.225 ExampleRunner[42526:707] {
objs = (
a,
b,
c
);
}
2012-03-28 13:49:46.225 ExampleRunner[42526:707] __NSArrayM
编辑
请注意,如果我们将以下行附加到上面的代码中...
Note that if we append the following line to the code above...
NSLog(@"%@", [[dict objectForKey:@"objs"] superclass]);
...我们在控制台上获得以下输出:
...we get the following output on the console:
2012-03-28 18:09:53.770 ExampleRunner[42830:707] NSMutableArray
...以防万一 __ NSArrayM
是 NSMutableArray <的私有子类/ code>,从而证明OP的代码确实按预期工作(除了他的
NSLog
语句)。
...just in case it wasn't clear that __NSArrayM
is a private subclass of NSMutableArray
, thus proving that the OP's code did indeed work as expected (except for his NSLog
statement).
编辑
哦,顺便说一下,以下代码行......
Oh, and by the way, the following line of code...
NSLog(@"%d", [[dict objectForKey:@"objs"] isKindOfClass:[NSMutableArray class]]);
...导致以下控制台输出:
...results in the following console output:
2012-03-28 18:19:19.721 ExampleRunner[42886:707] 1
编辑(回复已更改的问题)
有趣......看起来像个错误。给出以下代码:
Interesting...looks like a bug. Given the following code:
NSData *dictData2 = [@"{ \"foo\": \"bar\" }" dataUsingEncoding:NSUTF8StringEncoding];
id dict2 = [NSJSONSerialization JSONObjectWithData:dictData2 options:NSJSONReadingMutableContainers error:NULL];
NSLog(@"%@", [dict2 class]);
NSLog(@"%@", [dict2 superclass]);
NSLog(@"%d", [dict2 isKindOfClass:[NSMutableDictionary class]]);
// This works...
[dict2 setObject:@"quux" forKey:@"baz"];
NSLog(@"%@", dict2);
NSData *dictData = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
id emptyDict = [NSJSONSerialization JSONObjectWithData:dictData options:NSJSONReadingMutableContainers error:NULL];
NSLog(@"%@", [emptyDict class]);
NSLog(@"%@", [emptyDict superclass]);
NSLog(@"%d", [emptyDict isKindOfClass:[NSMutableDictionary class]]);
//...but this fails:
[emptyDict setObject:@"quux" forKey:@"baz"];
NSLog(@"%@", emptyDict);
这是控制台输出:
2012-03-29 09:40:52.781 ExampleRunner[43816:707] NSMutableDictionary
2012-03-29 09:40:52.782 ExampleRunner[43816:707] 1
2012-03-29 09:40:52.782 ExampleRunner[43816:707] __NSCFDictionary
2012-03-29 09:40:52.782 ExampleRunner[43816:707] NSMutableDictionary
2012-03-29 09:40:52.783 ExampleRunner[43816:707] 1
2012-03-29 09:40:52.783 ExampleRunner[43816:707] {
baz = quux;
foo = bar;
}
2012-03-29 09:40:52.784 ExampleRunner[43816:707] __NSCFDictionary
2012-03-29 09:40:52.784 ExampleRunner[43816:707] NSMutableDictionary
2012-03-29 09:40:52.784 ExampleRunner[43816:707] 1
2012-03-29 09:40:52.785 ExampleRunner[43816:707] NSException: -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
因此以这种方式创建的空数组和字典似乎没有按预期运行。
So empty arrays and dictionaries created this way don't seem to behave as expected.
这篇关于NSJSONSerialization没有创建可变容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!