xcode NSJSONSerialization 导致 EXC_BAD_ACCESS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12842481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSJSONSerialization results in EXC_BAD_ACCESS
提问by Christian Hennen
Currently I am writing an app (Target iOS 6, ARC enabled) that uses JSON for data transmission and Core Data for persistent storage. The JSON data is generated out of a MySQL database by a PHP script via json_encode.
目前我正在编写一个应用程序(目标 iOS 6,启用 ARC),它使用 JSON 进行数据传输,使用核心数据进行持久存储。JSON 数据由 PHP 脚本通过 json_encode 从 MySQL 数据库中生成。
My Problem is that with data from certain tables the following code fails:
我的问题是,对于某些表中的数据,以下代码失败:
- (NSDictionary *)executeFetch:(NSString *)query
{
NSURL *requesturl = [NSURL URLWithString:[query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *dataError = nil;
self.jsonData = [NSData dataWithContentsOfURL:requesturl options:kNilOptions error:&dataError];
NSError *error = nil;
self.jsonSerializationResult = [NSJSONSerialization JSONObjectWithData:self.jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];
return self.jsonSerializationResult;
}
The program always crashes with an EXC_BAD_ACCESS error on the line where it says self.jsonSerializationResult and Instruments says that there was a Zombie detected. I know that this means that some object I send a message to is nil, but I can't find out how to fix it... That's what Instruments has to say:
程序总是崩溃,并在上面说 self.jsonSerializationResult 和 Instruments 说检测到僵尸的行上出现 EXC_BAD_ACCESS 错误。我知道这意味着我向其发送消息的某个对象为零,但我不知道如何修复它......这就是 Instruments 所说的:
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0xa1b8a70 CFString (mutable) Malloc 1 00:01.603.081 32 Foundation -[NSPlaceholderMutableString initWithBytesNoCopy:length:encoding:freeWhenDone:]
1 0xa1b8a70 CFString (mutable) Release 0 00:01.603.137 0 Foundation newJSONValue
2 0xa1b8a70 CFString (mutable) Zombie -1 00:01.603.259 0 Foundation newJSONString
My program works with every JSON output except for this one:
我的程序适用于除此之外的所有 JSON 输出:
{
"termin":[
{
"termin_id":"17",
"veranstaltung_id":"20",
"beginn":"2012-09-28 17:00:00",
"ende":"2012-09-28 18:00:00",
"freie_pl\u00e4tze":null
},
{
"termin_id":"18",
"veranstaltung_id":"26",
"beginn":"2012-09-28 19:00:00",
"ende":"2012-09-28 20:00:00",
"freie_pl\u00e4tze":null
},
{
"termin_id":"19",
"veranstaltung_id":"26",
"beginn":"2012-09-28 21:00:00",
"ende":"2012-09-28 22:00:00",
"freie_pl\u00e4tze":null
},
{
"termin_id":"20",
"veranstaltung_id":"46",
"beginn":"2012-09-28 19:00:00",
"ende":"2012-09-28 20:00:00",
"freie_pl\u00e4tze":null
},
{
"termin_id":"24",
"veranstaltung_id":"66",
"beginn":"2012-09-28 22:00:00",
"ende":"2012-09-28 22:30:00",
"freie_pl\u00e4tze":"120"
}
]
}
I thought about some possible error sourced but none seems to be the responsible:
我想到了一些可能的错误来源,但似乎没有一个是负责任的:
- jsonData or jsonSerializationResult could be nil: they aren't
- PHP generated invalid JSON: checked for that with a validator
- null Values: not a problem with other tables
- jsonData 或 jsonSerializationResult 可能为零:它们不是
- PHP 生成了无效的 JSON:使用验证器进行检查
- null 值:其他表没有问题
Has anybody got an idea?
有人有想法吗?
回答by user1071136
It looks like a bug/shortcoming with NSJSONSerialization
. The problem is caused by the escaped unicode characters (freie_pl\u00e4tze
instead of freie_pl?tze
). You have two options -
它看起来像一个错误/缺点NSJSONSerialization
。问题是由转义的 unicode 字符(freie_pl\u00e4tze
而不是freie_pl?tze
)引起的。你有两个选择——
- Convert the escaped Unicode to real Unicode characters. Try this SO answer
- Use another JSON engine, such as JSONKit.
JSONKit
also claims to be more performant thanNSJSONSerialization
.
回答by Max
I know this question has been answered but I think some beginners may have the same issue as me and be brought to this question.
我知道这个问题已经得到回答,但我认为一些初学者可能和我有同样的问题并被带到这个问题。
The EXC_BAD_ACCESS message was caused by malformed JSON. As I had accidentally used the same name for an Object which causes issues when converting the JSON into a dictionary.
EXC_BAD_ACCESS 消息是由格式错误的 JSON 引起的。因为我不小心对一个对象使用了相同的名称,这在将 JSON 转换为字典时会导致问题。
Annoyingly it didn't bring up a formatting error. Here is an example of the JSON that caused the issue:
令人讨厌的是,它没有出现格式错误。以下是导致问题的 JSON 示例:
"levels" : {
"level1": {
....
},
"level1": {
... << All objects should have different names. This should be called level2.
},
"level3": {
...
}
To fix the issue I had to ensure that all objects of the same level had different names.
为了解决这个问题,我必须确保同一级别的所有对象都有不同的名称。
回答by Xiaochen Du
Just tested NSJSONSerialization today. With iOS 7.1. It is working. No issue found. Looks like Apple fixed the issue.
今天刚刚测试了 NSJSONSerialization。使用 iOS 7.1。这是工作。没有发现问题。看起来苹果解决了这个问题。
NSString* jsonString = @"{ \"freie_pl\u00e4tze\":null}";
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary* jsonSerializationResult = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];
NSLog(@"%@", jsonSerializationResult);