5

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

TimerEvent *latestTappedEvent = [[TimerEvent alloc] init];
latestTappedEvent.timeTapped = NSDate.date;
latestTappedEvent.activityTapped = tappedItem.itemName;

这两个数据片段必须保持相互关联。

我的问题是这样的:

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

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

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

4

1 回答 1

2

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

Index       Storage

0           "some string stored at index 0"

1           "some other string"

2           <some other object, stored at index 2>

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

字典使用任意对象进行查找,这也意味着字典中没有排序,只有一组键的关联以及它们所指的内容。以表格形式:

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"

要从"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>

再一次,字典,同一张表:

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"

另外,让我们TimerEvent在同一张表中查看您的课程:

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

timeTapped                                                  <date object>

activityTapped                                              "name of an activity"

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

Key                 Object

"timeTapped"        <date object>

"activityTapped"    "activity name"

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

NSDictionary *timerEventRepresentation = @{ @"timeTapped": timerEvent.timeTapped,
                                            @"activityTapped": timerEvent.activityTapped};

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

TimerEvent *timerEvent = [[TimerEvent alloc] init];
timerEvent.timeTapped = timerEventDictionaryRepresentation[@"timeTapped"];
timerEvent.activityTapped = timerEventDictionaryRepresentation[@"activityTapped"];

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

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

Index        Item
0            <dictionary representing first tap>
1            <dictionary representing second tap>
...
n            <dictionary representing n-1th tap>

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

于 2013-10-29T02:12:48.543 回答