plists,数组和字典元素之间的关系是什么?

rat*_*p99 5 objective-c plist nsmutablearray nsmutabledictionary ios

我有一个简单的程序,需要记录并持久存储每次用户在表视图中选择项目时产生的2个简单数据.这两个数据是1)tap()的时间NSDate和2)tapped项目的名称(NSString).此时,此信息采用以下形式:

TimerEvent *latestTappedEvent = [[TimerEvent alloc] init];
latestTappedEvent.timeTapped = NSDate.date;
latestTappedEvent.activityTapped = tappedItem.itemName;
Run Code Online (Sandbox Code Playgroud)

这两个数据必须保持相互关联.

我的问题是:

如何按时间顺序将这些数据输入和输出plist?

在我的研究中,我只是变得更加困惑.对我来说,如何使用plist并不明显.最初,我认为我可以使用NSMutableDictionarywith latestTappedEvent.timeTapped作为键,latestTappedEvent.activityTapped作为值.但是当我尝试手动构建plist时,似乎不可能,而是想要一个字符串作为键.

如果有人能帮助我理解这一点,最好通过给出这些不同元素之间关系的图形表示,我将永远感激不尽.

Car*_*zey 2

字典和数组都存储“事物”,并且通过使用“其他事物”对数据结构进行“查找”来检索和设置存储的事物。在数组中,该查找是存储对象的数组中的索引。以表格形式:

Index       Storage

0           "some string stored at index 0"

1           "some other string"

2           <some other object, stored at index 2>
Run Code Online (Sandbox Code Playgroud)

要找到它,"some string stored at index 0"您需要知道它存储在索引 0 处,并向数组询问该索引处的对象。因此数组使用整数来查找存储在其中的对象,并且这些整数必须在 0 到数组计数减 1 的范围内。使用整数查找数组中的项目还给出了数组顺序 - 从上到下您在上表中看到的 -bottom 排序与代码中迭代产生的顺序相同。

字典使用任意对象进行查找,这也意味着字典中没有顺序,只有一组键及其引用的关联。以表格形式:

Key         Storage

"name"      "a string that will be accessed using the key 'name'"

"number"    <some numeric object, that will be accessed using the key 'number'>

<object>    "will be accessed with key <object> which is an arbitrary object"
Run Code Online (Sandbox Code Playgroud)

要从该字典中获取内容"a string that will be accessed using the key 'name'",您需要向字典询问 key 下存储的内容"name"

在上面的示例中,我给出了表标题“索引 - 存储”或“键 - 存储”,但要回到这些结构都存储使用其他东西访问的东西的观点,让我们用更通用的方式查看数组桌子:

Thing used to access the thing that's stored        Thing that's stored

0                                                   "some string stored at index 0"

1                                                   "some other string"

2                                                   <some other object, stored at index 2>
Run Code Online (Sandbox Code Playgroud)

再次,字典,具有相同的表:

Thing used to access the thing that's stored        Thing that's stored

"name"                                              "a string that will be accessed using the key 'name'"

"number"                                            <some numeric object, that will be accessed using the key 'number'>

<object>                                            "will be accessed with key <object> which is an arbitrary object"
Run Code Online (Sandbox Code Playgroud)

另外,让我们TimerEvent在同一个表中查看您的班级:

Thing used to access the thing that's stored                Thing that's stored

timeTapped                                                  <date object>

activityTapped                                              "name of an activity"
Run Code Online (Sandbox Code Playgroud)

左列中的项目是 Objective-C 属性名称,右列中的项目是这些属性包含的值。现在,再看一下字典 - 左边的项目是任意值(实际上它们通常是字符串),右边的项目是其他任意值。希望您能看到这里的联系 - 您通常可以将对象的属性表示为字典,将属性名称的字符串表示形式映射到属性存储的值。因此,如果您想TimerEvent在字典中表示该对象,您最终会得到如下表示:

Key                 Object

"timeTapped"        <date object>

"activityTapped"    "activity name"
Run Code Online (Sandbox Code Playgroud)

上表说明了数组、字典和其他对象之间的共性和差异,并表明使用字典将属性名称映射到属性值可以表示任何给定对象的属性。那么,执行此操作的代码是什么样子的呢?假设我们想要在 中表示TimerEvent对象:timerEventNSDictionary

NSDictionary *timerEventRepresentation = @{ @"timeTapped": timerEvent.timeTapped,
                                            @"activityTapped": timerEvent.activityTapped};
Run Code Online (Sandbox Code Playgroud)

下面是我们如何TimerEvent从字典表示中创建一个:

TimerEvent *timerEvent = [[TimerEvent alloc] init];
timerEvent.timeTapped = timerEventDictionaryRepresentation[@"timeTapped"];
timerEvent.activityTapped = timerEventDictionaryRepresentation[@"activityTapped"];
Run Code Online (Sandbox Code Playgroud)

将所有对象强制写入字典的目的是属性列表格式仅序列化几个类 - NSArrayNSDictionaryNSStringNSDateNSNumberNSData。因此,我们编写代码来使用支持的类来表示不支持的类,反之亦然,以在 plist 中序列化这些对象。

作为附录,您提到您需要存储所有点击的记录并对它们进行排序。正如我上面提到的,数组本质上对它们存储的内容进行排序,因此这是合适的解决方案。你想要构建一个看起来像这样的东西:

NSDictionary *timerEventRepresentation = @{ @"timeTapped": timerEvent.timeTapped,
                                            @"activityTapped": timerEvent.activityTapped};
Run Code Online (Sandbox Code Playgroud)

在代码中,序列化每个水龙头将采用与前面描述的相同的形式,但请确保添加一个额外的步骤,addObject:NSMutableArray使用新创建的字典作为参数来调用属性。