后端和 Objective-C/Cocoa 之间通信的格式
我正在开发一个连接到后端服务器的 iPhone 应用程序。它需要通过多个请求与它进行多次通信。我正在发送 HTTP 消息,但我想接收可以以某种方式解析的更复杂的响应。据说,我可以为来自服务器的响应提供任何类型的格式,所以我的问题是:哪种格式更容易(甚至更快)用于 Objective-C/Cocoa 使用/解析,并在某种字典中进行转换?
我知道这有点主观,但我仍然认为这是一个有效的问题,某些编程语言只是对某些格式的支持更多,而对其他格式的支持更少。
I'm developing an iPhone app that is connected to a backend server. It needs to communicate with it many times, through several requests. I'm sending HTTP messages, but I want to receive more complex responses that I can parse somehow. Supposedly, I can provide any type of format for responses from the server, so my question is: which one would be easier(maybe even faster) to use/parse for Objective-C/Cocoa, and transform in a dictionary of some kind?
I know it's a bit subjective but I still think it's a valid question, some programming languages just have more support for some formats and less for others.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Cocoa 的角度来看,最简单的格式是 属性列表,因为 Cocoa 可以将其本地解析为字典或数组。
您可以使用 NSDictionary 的
+dictionaryWithContentsOfFile:
和+dictionaryWithContentsOfUrl:
将 plist 文件读入字典中。如果您的 plist 数据不在文件中,您还可以使用
+[NSPropertyListSerialization dataFromPropertyList:format:errorDescription:]
将包含 plist 数据的 NSData 对象转换为字典,或使用将 NSString 转换为字典代码>-[NSString 属性列表]。
From Cocoa 's perspective, the simplest format is a property list as Cocoa can parse this natively into a dictionary or an array.
You can use NSDictionary's
+dictionaryWithContentsOfFile:
and+dictionaryWithContentsOfUrl:
to read a plist file into a dictionary.If your plist data is not in a file, you can also convert an NSData object containing plist data to a dictionary with
+[NSPropertyListSerialization dataFromPropertyList:format:errorDescription:]
or convert an NSString to a dictionary with-[NSString propertyList]
.PList 是一个很好的答案并且非常有用,但是许多服务器端人员会更舒服地生成 JSON - TouchJSON 是一个非常好的 iPhone 的 JSON 解析器。
PList is a good answer and very usable, but many server side people will be more comfortable producing JSON - TouchJSON is a very good JSON parser for the iPhone.
虽然有 plist gem for ruby、JSON 或(原始)XML 在 Apple 世界之外更受欢迎。例如,大多数 JavaScript 库都设置为使用其中之一或两者。
因此,如果您专门与 iPhone 通信,plist 可能是一个不错的选择,但否则您应该考虑使用 JSON(或 XML)。
While there is a plist gem for ruby, JSON or (raw) XML are much more popular outside the Apple world. For instance most JavaScript libraries are set up to speak one or both of these.
So if you're exclusively talking to an iPhone, the plist is probably a good choice, but otherwise you should consider using JSON (or XML).