字典和数组都存储“东西”- 存储的东西是通过使用“其他东西”在数据结构上进行“查找”来检索和设置的。在数组中,该查找是存储对象的数组索引。以表格形式表示:
Index Storage
0 "some string stored at index 0"
1 "some other string"
2 <some other object, stored at index 2>
要查找存储在索引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"
要从这个字典中获取使用键“name”访问的字符串,您需要请求存储在键“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"
上面的表格说明了数组、字典和其他对象之间的共同点和差异,并展示了使用字典将属性名映射到属性值可以表示任何给定对象的属性。那么,如何编写代码来实现这一点呢?假设我们想要在NSDictionary中表示TimerEvent对象timerEvent:
NSDictionary *timerEventRepresentation = @{ @"timeTapped": timerEvent.timeTapped,
@"activityTapped": timerEvent.activityTapped}
以下是我们如何从字典表示中创建一个TimerEvent
:
TimerEvent *timerEvent = [[TimerEvent alloc] init];
timerEvent.timeTapped = timerEventDictionaryRepresentation[@"timeTapped"];
timerEvent.activityTapped = timerEventDictionaryRepresentation[@"activityTapped"];
将所有对象强制转换为字典的目的是属性列表格式仅序列化少数类 -
NSArray
,
NSDictionary
,
NSString
,
NSDate
,
NSNumber
和
NSData
。因此,我们编写代码使用支持的类来表示不支持的类,反之亦然,以便在属性列表中序列化这些对象。
补充一下,您提到需要存储所有点击记录并对其进行排序。如上所述,数组固有地对其存储的内容进行排序,因此这是适当的解决方案。您需要构建类似于以下内容的东西:
Index Item
0 <dictionary representing first tap>
1 <dictionary representing second tap>
...
n <dictionary representing n-1th tap>
在代码中,对每个tap进行序列化的形式与之前描述的相同,但要确保添加一个额外的步骤,即在一个
NSMutableArray
属性上调用
addObject:
方法,并将新创建的字典作为参数传递进去。
TimerEvent
对象存储到某个集合中,而不是单独存储.timeTapped
和.activityTapped
? - nhgrif