我正在尝试创建一个NSMutableDictionary,并使用从我的网站查询的论坛帖子填充它。超级简单的代码,只不过......
_postBodies = [[NSMutableDictionary alloc] initWithContentsOfURL:[NSURL URLWithString:urlBodiesSource]];
...从PHP / MySQL生成的plist文件中提取,通常效果很好。事实上,如果任何帖子包含某些字符,例如非英文字母或HTML符号等,则整个事情都无法完全初始化。我已经在调试器中逐行跟踪它,我可以直接在源文件中看到它与34个帖子(或其他)一起工作,但是将第35个帖子与奇怪的字符添加到plist中并且整个事情都失败了初始化。
有解决方法吗?一些剥离或转义字符的方法对NSMutableDictionary有问题?或者也许他们可能会绊倒我的plist文件,而且词典工作得很好?
例如,请查看this plist source file。它只有三个帖子长,最后一个是问题。排除那个帖子,整个事情都有效;包含它并且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>
以前有没有人经历过这个,或者有人可以提供任何建议吗?我刚刚开始这么简单的解决方案会受到赞赏,但我喜欢你能提供的任何见解!
答案 0 :(得分:0)
您是否正在检查PList文本编码?请记住,iOS编程是以UTF-8完成的。
我假设(因为问题标有PHP标记),您要检索的URL与PHP一起使用。您是否尝试使用 utf8_encode()?
对文字进行编码另一种方法可能是使用JSON。 PHP中的 json_encode()会自动将奇怪的字符转换为其UTF-8十六进制表示形式( \ x {NUMBER} )。此外,您可以尝试使用 htmlspecialchars()。
祝你好运!