本文介绍了NSJSONSerialization导致EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在编写一个应用程序(目标iOS 6,启用了ARC),它使用JSON进行数据传输,使用Core Data进行持久存储。 JSON数据是由PHP脚本通过json_encode在MySQL数据库中生成的。

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.

我的问题是,对于某些表中的数据,以下代码失败:

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;

}

程序总是因行中出现EXC_BAD_ACCESS错误而崩溃它说self.jsonSerializationResult和Instruments说有一个Zombie被发现了。我知道这意味着我发送消息的对象是零,但我无法找到如何解决它...这就是乐器所说的:

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:

#   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

我的程序适用于除此之外的每个JSON输出:

My program works with every JSON output except for this one:

{
   "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或jsonSerializationResult可能是nil:它们不是

  • PHP生成的无效JSON:使用验证器检查了它

  • null值:其他表没有问题

有没有人知道?

推荐答案

这看起来像一个错误/缺点使用 NSJSONSerialization 。问题是由转义的unicode字符引起的( freie_pl\\\ätze 而不是freie_plätze)。您有两个选择 -

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 -


  1. 将转义的Unicode转换为真正的Unicode字符。试试

  2. 使用其他JSON引擎,例如 JSONKit JSONKit 也声称比 NSJSONSerialization 更具性能。

  1. Convert the escaped Unicode to real Unicode characters. Try this SO answer
  2. Use another JSON engine, such as JSONKit. JSONKit also claims to be more performant than NSJSONSerialization.

这篇关于NSJSONSerialization导致EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 09:39