我如何排序的属性值包含NSManagedObject子类的实例迅速阵列(日期) [英] How do I sort a swift array containing instances of NSManagedObject subclass by an attribute value (date)

查看:112
本文介绍了我如何排序的属性值包含NSManagedObject子类的实例迅速阵列(日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想数组排序在<一个布局href=\"http://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value\">the接受了这个问题的答案,但我跑成Isuru提到了关于这个问题的答案的评论的问题。也就是说,code应该由实体的日期属性中的数组进行排序带来的编译器抱怨找不到成员'日期'。

I'm trying to sort an array as laid out in the accepted answer to this question, but am running into the problem which Isuru mentions in the comments on that answer. Namely, the code which should sort the array by the entity's "date" attribute brings the compiler complaint "could not find member 'date'"

下面是NSManagedObject子类描述实体:

Here is the NSManagedObject subclass describing the entity:

import Foundation
import CoreData

@objc(Entry)
class Entry: NSManagedObject {

    @NSManaged var date: NSDate
    @NSManaged var reflections: AnyObject
    @NSManaged var contactComment: NSSet
    @NSManaged var person: NSSet

    override func awakeFromInsert() {
        let now:NSDate = NSDate()
        self.date = now;
    }
}

这里是它试图数组排序code:

And here is the code which tries to sort the array:

lazy var entries:[Entry] = {
    var days:[Entry] = self.managedObjectContext!.requestEntity("Entry")as [Entry]
    days.sort({$0.date < $1.date})

    var today:Entry = days.last!
    println(today.date)
    return days
}()

请注意,在这code的后半部分,我能为条目之一访问和日志日期属性,编译器不会有问题吧。

Note that in the latter part of that code, I am able to access and log the "date" property for one of the entries, and the Compiler doesn't have a problem with it.

是我的排序正确的语法?是否与此code,我没有看到另外一个问题?

Is my syntax for sorting correct? Is there another issue with this code I'm not seeing?

推荐答案

这部分是与雨燕编译器不会给你一个有用的错误的问题。真正的问题是,的NSDate 不能&LT进行比较; 直接。相反,你可以使用的比较方法,像这样的NSDate

This is partly an issue with the Swift compiler not giving you a helpful error. The real issue is that NSDate can't be compared with < directly. Instead, you can use NSDate's compare method, like so:

days.sort({ $0.date.compare($1.date) == NSComparisonResult.OrderedAscending })

另外,你可以扩展的NSDate 实施可比协议,这样它可以与<$ C进行比较$ C>&LT; (和&LT; = &GT; &GT; = ==

Alternatively, you could extend NSDate to implement the Comparable protocol so that it can be compared with < (and <=, >, >=, ==):

public func <(a: NSDate, b: NSDate) -> Bool {
    return a.compare(b) == NSComparisonResult.OrderedAscending
}

public func ==(a: NSDate, b: NSDate) -> Bool {
    return a.compare(b) == NSComparisonResult.OrderedSame
}

extension NSDate: Comparable { }

注意:您只需要执行&LT; == 上面显示,那么运营的其余&LT; = &GT; 等,将被提供标准库。

Note: You only need to implement < and == and shown above, then rest of the operators <=, >, etc. will be provided by the standard library.

通过在地方,你原来的排序功能应该只是罚款:

With that in place, your original sort function should work just fine:

days.sort({ $0.date < $1.date })

这篇关于我如何排序的属性值包含NSManagedObject子类的实例迅速阵列(日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆