如何编写swift3语法?
 Swift3 AnyObject修改,无功能

swift2:

  func uniqueValueForObject<U : Equatable>(objectKey: String, paramKey: String, type: U.Type)->[U]{
    var uniqueValues : [U] = [U]()
    for obj in self {

        if let o = obj.valueForKeyPath(forKeyPath: objectKey) {
            o.
            if let v = o.valueForKeyPath(paramKey){

                if(!uniqueValues.contains(v as! U)){
                    uniqueValues.append(v as! U)
                }

            }
        }

    }
    return uniqueValues
}


有人可以帮忙写swift3吗?

最佳答案

如果从字面上看这仅仅是为Swift 3更新Realm Swift 2语法的问题,那么它应该看起来像这样:

extension Results {
    func uniqueValueForObject<U: Equatable>(objectKey: String, paramKey: String, type: U.Type) -> [U] {
        var uniqueValues: [U] = [U]()
        for obj in self {
            if let o = obj.value(forKeyPath: objectKey) {
                if let v = (o as AnyObject).value(forKeyPath: paramKey) {

                    if !uniqueValues.contains(v as! U) {
                        uniqueValues.append(v as! U)
                    }
                }
            }
        }
        return uniqueValues
    }
}

关于ios - Realm uniqueValueForObject Swift3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41560858/

10-10 21:01