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

标签 iphone ios xcode ipad plist

我有一个包含字典的自定义 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/

相关文章:

Xcode Bot 集成错误 : Failed to mmap

iphone - 日期更改后的 UIDatePicker

iphone - 在导航栏中添加分段控件

ios - 在 Xcode 6 机器人上选择分支

ios - 终止我的应用程序后如何保持与 mqtt 代理的连接(ios,Swift)

ios - 单例方法停止 iOS 中的代码执行

ios - Xcode - Swift - TableViewCell 扩展中的 UILabel

ios - 如何在 iOS 13 上获取给定网络接口(interface)的当前网络信息

iOS 为什么在设置 NSBaselineOffsetAttributeName 时 NSTextAttachment 会消失?

iphone - Xcode - 获取所有方法头的列表