如果包含某些字符,NSMutableDictionary未初始化


NSMutableDictionary not initializing if certain characters included

我正在尝试创建一个NSMutableDictionary并填充从我的网站查询论坛帖子。超级简单的代码,除了…

_postBodies = [[NSMutableDictionary alloc] initWithContentsOfURL:[NSURL URLWithString:urlBodiesSource]];

…从PHP/mysql生成的plist文件中提取,它通常工作得很好。问题是,如果任何帖子包含某些字符,例如非英语字母或HTML符号等,则整个内容将无法初始化。我已经在调试器中逐行跟踪了它,并且我可以在源文件中看到它与34个帖子(或其他)一起工作,但是将带有奇怪字符的第35个帖子添加到plist中,整个事情无法初始化。

有办法解决这个问题吗?一些剥离或转义字符的方法这对NSMutableDictionary来说是有问题的?或者他们可能会破坏我的plist文件,而Dictionary工作正常?

例如,查看这个plist源文件。它只有三个帖子长,最后一个是问题。排除那个帖子,整个事情就有效了;包括它,NSMutableDictionary就无法填充。它看起来很正常,除了它有一些制表符和一些带有转圈的O。

<key>2</key>
<string>Wow, SquattingB.  I know I'm melodramatic, but what's with you?  lol.  Yeah, it's a shame that all the cool people congregated here just to watch the codex, only to have the series end and leave everyone dumbstruck as to what to do.  (It was that good, nothing else has the same appeal)  Hopefully this forum will remain as cool as it has been up to the point when the creators get out of college.  Then maybe we can see whether they have awesome news of what is next for the series and the forum or if they just boot us back to HBO.  lol.But what is a forum?Dictionary   forum |?fôr?m| noun ( pl. forums ) 1 a place, meeting, or medium where ideas and views on a particular issue can be exchanged : it will be a forum for consumers to exchange their views on medical research. 2 a court or tribunal. 3 ( pl. fora |?fôr?|) (in an ancient Roman city) a public square or marketplace used for judicial and other business. ORIGIN late Middle English (sense 3) : from Latin, literally ‘what is out of doors,’ originally denoting an enclosure surrounding a house; related to fores ‘(outside) door.’ Sense 1 dates from the mid 18th cent.That's what I thought.  So the people are what make it great, yes?  And I'm sure the patrons aren't leaving anytime soon, so we're in pretty good standing as to what will become of the forum.  For now, anyway.</string>

有人经历过这种情况吗,或者有人能提供一些建议吗?我才刚刚开始,所以更简单的解决方案会很感激,但我希望你能提供任何见解!

是否检查PList文本编码?请记住,iOS编程是用UTF-8完成的。

我假设(因为问题用PHP标记),您正在检索的URL适用于PHP。您是否尝试使用utf8_encode()对文本进行编码?

另一种方法是使用JSON。PHP中的json_encode()会自动将奇怪的字符转换为UTF-8十六进制表示('x{NUMBER})。此外,您可以尝试使用htmlspecialchars()

祝你好运!

相关文章:
  • 没有找到相关文章