swift3 – swift 3,PHFetchResult.enumerateObjects错误

前端之家收集整理的这篇文章主要介绍了swift3 – swift 3,PHFetchResult.enumerateObjects错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
swift 3中,该方法向我展示了“模糊地使用’enumerateObjects’”,发生了什么.我该怎么办?
extension PHFetchResult {
public func assetCollection() -> [PHAssetCollection] {
    var list :[PHAssetCollection] = []
    self.enumerateObjects { (object,index,stop) in
        if object is PHAssetCollection {
            let collection = object as! PHAssetCollection
            list.append(collection)
        }
    }
    return list
}
}
做类似这样的事.您不能直接为PHFetchResult添加扩展名,因为它有其他ObjectType作为其通用参数PHFetchResult< ObjectType> .所以你必须做点别的事.
class FetchPhoto {

    class func assetCollection() -> [PHAssetCollection] {
        var list :[PHAssetCollection] = []

        PHAssetCollection.fetchMoments(with: nil).enumerateObjects(EnumerationOptions.concurrent) { (collection,_,_) in
                list.append(collection)
        }

        return list
    }
}

PHAssetCollection.fetchMoments返回PHFetchResult< PHAssetCollection>这里PHAssetCollection是PHFetchResult的ObjectType.您收到了模糊错误,因为您尚未指定objectType.

解决这个问题的一般方法.

class FetchPhoto {

    class func assetCollection<T : PHObject>(result : PHFetchResult<T>) -> [T] {

        var list : [T] = []

        result.enumerateObjects(EnumerationOptions.concurrent) { (object,_) in
            list.append(object)
        }

        return list
    }
}

斯威夫特3

class PhotosHelper {

        class func fetchAllLocalIdentifiersOfPhotos(completion : (_ localIdentifiers : [String]) -> ()) {

            let photos : PHFetchResult<PHAsset> = PHAsset.fetchAssets(with: PHAssetMediaType.image,options: nil)
            photos.enumerateObjects ({ _,_ in

// Do your operations,you can see that there is no warnings/errors in this one
            })

        }
    }
原文链接:https://www.f2er.com/swift/319293.html

猜你在找的Swift相关文章