NSDictionary与。 NSArray的 [英] NSDictionary Vs. NSArray

查看:84
本文介绍了NSDictionary与。 NSArray的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读objective-c(一本书呆子牧场书),我不禁想到这个问题:我如何决定哪种集合类型,NSArray或NSDictionary(两者都带有或者没有可变子类) ,从URL读取内容时使用?

I am reading on objective-c (a nerd ranch book), and I can't help thinking about this question: How do I decide which collection type, NSArray or NSDictionary (both with or w/o their mutable subclasses), to use when reading content from URL?

假设我正在从PHP脚本(正在处理的场景)中读取JSON数据,哪些要使用?我知道在许多参考文献中都说它依赖于数据结构(即JSON),但是可以概括出两种结构的轮廓吗?

Let's say am reading JSON data from a PHP script (a scenario am dealing with), which to use? I know it is stated in many references that it depends on structure of data (i.e. JSON), but could a clear outline of the two structures be outlined?

谢谢大家帮助:)

推荐答案

NSArray 基本上只是一个有序的集合对象,可以通过索引访问。

NSDictionary 提供按键访问其对象(通常是NSStrings,但可以是任何对象类型,如哈希表)。

NSArray is basically just an ordered collection of objects, which can be accessed by index.
NSDictionary provides access to its objects by key(typically NSStrings, but could be any object type like hash table).

要从通过URL加载的JSON字符串生成对象图,可以使用 NSJSONSerialization ,它会生成Objective-C对象结构。生成的对象取决于JSON字符串。如果JSON中的顶级元素是一个数组(以 [ 开头),您将获得一个NSArray。如果顶级元素是JSON对象(以 { 开头),您将获得NSDictionary。

To generate an object graph from a JSON string loaded via a URL, you use NSJSONSerialization, which generates an Objective-C object structure. The resulting object depends on the JSON string. If the top-level element in your JSON is an array (starts with "["), you'll get an NSArray. If the top-level element is a JSON object (starts with "{"), you'll get an NSDictionary.

这篇关于NSDictionary与。 NSArray的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆