简体   繁体   中英

Non-Optional expression of type used in check for optionals

let rootInfo = realm.objects(StoreRealM.self).filter(msg)
    var root = [StoreRealM]()            
    for i in 0 ..< rootInfo.count {
        if let result = rootInfo[i] as? StoreRealM {
            root.append(result)
        }
    }

if let result = rootInfo[i] as? StoreRealM if let result = rootInfo[i] as? StoreRealM on this line I get it. I also get

Conditional cast from 'StoreRealM' to 'StoreRealM' always succeeds

Since root only can contain StoreRealM objects (guaranteed by the compiler), you do not need to cast the index access to this type; even more, it cannot contain Optional<StoreRealM> objects.

So it should be sufficient to write the for loop:

for i in 0 ..< rootInfo.count {
    let result = rootInfo[i]
    root.append(result)
}

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