简体   繁体   中英

realm uniqueValueForObject Swift3

How to write swift3 syntax? Swift3 AnyObject Modified,no function

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
}

Can anyone help write swift3?

If this is literally just a matter of updating the Realm Swift 2 syntax for Swift 3, then it should just look like this:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM