循环遍历字典;为什么我不能按字母顺序获取密钥?

发布于 2024-11-15 18:56:42 字数 1684 浏览 2 评论 0原文

我正在使用 S7Graphview 编写一个 iPhone 应用程序,并且我已将一些日期和结果保存到 .plist 作为字典中的键和值(两者都作为字符串)。我的 plist 如下所示:

<dict>
<key>2011-05-11</key>
<string>23</string>
<key>2011-05-12</key>
<string>23</string>
<key>2011-05-13</key>
<string>23</string>
<key>2011-05-14</key>
<string>43</string>
<key>2011-06-14</key>
<string>43</string>
</dict>

然后我使用此循环将这些值加载到 graphview 中:

NSMutableDictionary* dictionary = [[NSMutableDictionary alloc]init];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
int i = 1;        

if ([dictionary count] > 0) {
for (NSString* key in dictionary){
    NSString* verdiString = [dictionary objectForKey:key];
    CGFloat verdi = [verdiString intValue];
    NSDate *dato = [[NSDate alloc]init];
    NSLog(@"key=%@ value=%@", key, [dictionary objectForKey:key]);
    dato = [self stringTilDato:key];
    [items.list_ addObject:[[GraphInfo alloc] initWithID:i name:verdiString value:verdi date:dato]];
    i++;
}
}

“stringTilDato”方法将日期字符串转换为 NSDate。这些值被加载到 items.list 中,但顺序错误! NSLog 报告:(

key=2011-05-14 value=43
key=2011-05-13 value=23
key=2011-05-12 value=23
key=2011-06-14 value=43
key=2011-05-11 value=23
key=2011-05-14 value=43
key=2011-05-13 value=23
key=2011-05-12 value=23
key=2011-06-14 value=43
key=2011-05-11 value=23

顺便说一句,不知道为什么它会两次通过密钥,但我认为这并不重要)。我认为按键将按字母顺序读取,或者至少按 plist 的顺序读取。为什么 plist 会按这个顺序加载到字典中,或者是我的加载循环有问题?

希望有人可以帮助我:)

I am writing an iPhone app using S7Graphview and I have saved some dates and results to a .plist as keys and values in a dictionary, both as strings. My plist looks like this:

<dict>
<key>2011-05-11</key>
<string>23</string>
<key>2011-05-12</key>
<string>23</string>
<key>2011-05-13</key>
<string>23</string>
<key>2011-05-14</key>
<string>43</string>
<key>2011-06-14</key>
<string>43</string>
</dict>

Then I use this loop to load those values into the graphview:

NSMutableDictionary* dictionary = [[NSMutableDictionary alloc]init];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
int i = 1;        

if ([dictionary count] > 0) {
for (NSString* key in dictionary){
    NSString* verdiString = [dictionary objectForKey:key];
    CGFloat verdi = [verdiString intValue];
    NSDate *dato = [[NSDate alloc]init];
    NSLog(@"key=%@ value=%@", key, [dictionary objectForKey:key]);
    dato = [self stringTilDato:key];
    [items.list_ addObject:[[GraphInfo alloc] initWithID:i name:verdiString value:verdi date:dato]];
    i++;
}
}

The "stringTilDato" method converts the date string to a NSDate. The values get loaded into the items.list, but in the wrong order! The NSLog reports:

key=2011-05-14 value=43
key=2011-05-13 value=23
key=2011-05-12 value=23
key=2011-06-14 value=43
key=2011-05-11 value=23
key=2011-05-14 value=43
key=2011-05-13 value=23
key=2011-05-12 value=23
key=2011-06-14 value=43
key=2011-05-11 value=23

(Don't know why it goes through the keys twice, btw, but I dont't believe that's important). I thought the keys would be read alphabetically, or at least in the order of the plist. Why does the plist get loaded into the dictionary in this order, or is it my loading loop that is the problem?

Hope there is someone out there who can help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柳若烟 2024-11-22 18:56:42

字典不是有序的数据结构。您可以假设键没有特定的顺序,并且添加或删除键可以完全更改顺序。如果您需要有序的数据结构,请使用数组,或者在获取所有键后对它们进行排序。

A dictionary is not an ordered data structure. You can assume no particular order of the keys, and adding or removing a key can change the order completely. If you need an ordered data structure then use an array, or after getting all the keys sort them.

追星践月 2024-11-22 18:56:42

NSDictionary 的文档具体说明了:

数组中元素的顺序
未定义。

但是,如果您希望键按特定顺序排序,则至少有三种方法可以返回排序的键数组。查看 NSDictionary 参考页,了解以“keysSortedBy...”开头的方法

The documentation for NSDictionary says specifically:

The order of the elements in the array
is not defined.

However, if you want the keys sorted in a particular order, there are at least three methods that return a sorted array of keys. Look at the NSDictionary reference page for methods starting with "keysSortedBy..."

余厌 2024-11-22 18:56:42

字典不确保键输出有序。如果您希望键以特定顺序显示,则必须在访问它们的值并打印结果之前对它们进行排序。

Dictionaries do not ensure an ordered key output. If you want the keys displayed in a particular order, you will have to sort them prior to accessing their values and printing the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文