在
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> .所以你必须做点别的事.
原文链接:https://www.f2er.com/swift/319293.htmlclass 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 }) } }