gpt4 book ai didi

iphone - 来自 plist 文件的数组排序

转载 作者:行者123 更新时间:2023-11-28 20:35:51 24 4
gpt4 key购买 nike

我有一个包含字典的自定义 plist 文件。每本词典包含有关一辆汽车的信息:

     <key>Ford</key>
<dict>
<key>name</key>
<string>Ford</string>
<key>color</key>
<string>green</string>
<key>price</key>
<integer>45000</integer>
</dict

<key>Toyota</key>
<dict>
<key>name</key>
<string>Toyota</string>
<key>color</key>
<string>blue</string>
<key>price</key>
<integer>40000</integer>
</dict

<key>BMW</key>
<dict>
<key>name</key>
<string>BMW</string>
<key>color</key>
<string>blue</string>
<key>price</key>
<integer>40000</integer>
</dict>

我还有一个 UITableView,我用这个 plist 中的一些汽车填充它。

 NSEnumerator *enumerator = [dictionary objectEnumerator];
id returnValue;


while ((returnValue = [enumerator nextObject]))
{

if ([[returnValue valueForKey:@"color"]isEqualToString:@"blue")
{
[array addObject:[returnValue valueForKey:@"name"]];
}

问题:当我得到一个包含一些汽车的 UITableView 时,我现在如何按价格对它们进行排序?例如,BMW 比 Ford 便宜,所以它应该在第一个 cell 中,而 Ford 必须在第二个 cell 中。我知道 Apple 已经创建了对数组进行排序的简单方法,但我如何在这里使用它呢?谢谢 !最大

最佳答案

试试this :

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"price"  ascending:YES];
[array sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

关于iphone - 来自 plist 文件的数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com