排序CKRecord返回“预期类型”

时间:2019-03-20 11:44:40

标签: ios arrays swift sorting

为什么此代码在$ 1符号上返回期望的类型?

private func sorting(records: [CKRecord]) -> [CKRecord] {
    if ascendingSorting {
        return records.sorted {
            $0.value(forKey: "name") as! String < $1.value(forKey: "name") as! String
        }
    }
}

enter image description here

但是,如果我将“ <”更改为“>”,一切会顺利吗?

1 个答案:

答案 0 :(得分:1)

如果()没有触发,您只需要添加一个return和一个默认的if condition语句,请查看下面的代码。

private func sorting(records: [CKRecord]) -> [CKRecord] {
if ascendingSorting {
    return records.sorted {
        ($0.value(forKey: "name") as! String) < ($1.value(forKey: "name") as! String)
    }
}
//Missing return in a function expected to return '[CKRecord]'
return [] 
}