Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用comparator签名创建NSSortDescriptor?_Swift_Swift3_Nssortdescriptor - Fatal编程技术网

Swift 如何使用comparator签名创建NSSortDescriptor?

Swift 如何使用comparator签名创建NSSortDescriptor?,swift,swift3,nssortdescriptor,Swift,Swift3,Nssortdescriptor,我可以创建一个不区分大小写的字符串排序描述符,如下所示: let titleSort = NSSortDescriptor(key: "title", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare)) 我似乎不知道如何使用比较器签名: class NSSortDescriptor { init(key: St

我可以创建一个不区分大小写的字符串排序描述符,如下所示:

let titleSort = NSSortDescriptor(key: "title", ascending: true,
                                     selector: #selector(NSString.localizedCaseInsensitiveCompare))
我似乎不知道如何使用
比较器
签名:

class NSSortDescriptor {
  init(key: String?, ascending: Bool, comparator cmptr: @escaping Foundation.Comparator)
  ...
}
我必须从头开始创建一个新的比较器吗,还是
字符串的比较器已经存在

TIA

示例:

class C : NSObject {
    var id = 0
}

let desc = NSSortDescriptor(key: "id", ascending: true) { // comparator function
    id1, id2 in
    if (id1 as! Int) < (id2 as! Int) { return .orderedAscending }
    if (id1 as! Int) > (id2 as! Int) { return .orderedDescending }
    return .orderedSame
}

// test:

let c1 = C(); let c2 = C(); let c3 = C()
c1.id = 100; c2.id = 50; c3.id = 25
let arr = [c1,c2,c3]
let arr2 = (arr as NSArray).sortedArray(using: [desc])
C类:NSObject{
变量id=0
}
让desc=nssortddescriptor(键:“id”,升序:true){//comparator函数
id1,id2在
if(id1as!Int)<(id2as!Int){return.orderedascensing}
if(id1as!Int)>(id2as!Int){return.ordereddescenting}
退货。订购相同
}
//测试:
设c1=C();设c2=C();设c3=C()
c1.id=100;c2.id=50;c3.id=25
设arr=[c1,c2,c3]
设arr2=(arr作为NSArray.sortedArray(使用:[desc])

由于Comparator用于执行非内置的比较,因此没有全局函数。它非常灵活,因为它允许您比较两种不同类型的对象,并准确定义您希望它们如何排序。在一个非常简单的示例中,我创建了一个示例,首先检查传入的两个对象是否都是
String
,如果不是,我只是将它们视为应该在同一级别排序。否则,我会按字母顺序对它们排序(不区分大小写,因为我将它们改为小写)。显然,您可以根据您预期需要排序的内容,根据需要将其复杂化:

let descriptor = NSSortDescriptor(key: "title", ascending: true) { (string1, string2) -> ComparisonResult in
        guard let s1 = string1 as? String, let s2 = string2 as? String else {
            return ComparisonResult.orderedSame
        }
        if s1.lowercased() < s2.lowercased() {
            return ComparisonResult.orderedAscending
        } else if s1.lowercased() == s2.lowercased() {
            return ComparisonResult.orderedSame
        } else {
            return ComparisonResult.orderedDescending
        }
    }
let descriptor=NSSortDescriptor(键:“title”,升序:true){(string1,string2)->比较结果
将s1=string1设为字符串,将s2=string2设为其他字符串{
返回ComparisonResult.orderedSame
}
如果s1.lowercased()
您必须通过自己的
(任何,任何)->比较结果
结束。谢谢@MartinR。我不知道是否有一个全局函数可用于此目的。好的,让它工作,很抱歉第一次尝试出错。