字典和数组都存储“事物”——并且通过使用“其他事物”对数据结构进行“查找”来检索和设置存储的事物。在数组中,该查找是存储对象的数组中的索引。表格形式:
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'",您需要向字典询问"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。因此,我们编写代码来使用支持的类来表示不支持的类,反之亦然,以在 plist 中序列化这些对象。
作为附录,您提到您需要存储所有点击的记录,并对它们进行排序。正如我上面提到的,数组固有地对它们存储的东西进行排序,所以这是合适的解决方案。你会想要构建一个看起来像这样的东西:
Index Item
0 <dictionary representing first tap>
1 <dictionary representing second tap>
...
n <dictionary representing n-1th tap>
在代码中,序列化每个点击将采用与前面所述相同的形式,但请确保添加一个额外的步骤,即在 NSMutableArray 属性上以新创建的字典作为参数调用 addObject:。