Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIFT 3.0 migration error - Extension of a generic Obj-C class cannot access the class' generic parameter at runtime

I have this code working fine in Swift 2.

extension PHFetchResult: Sequence {
     public func makeIterator() -> NSFastEnumerationIterator {
         return NSFastEnumerationIterator(self)
     }
}

Since I upgraded to Swift 3

Extension of a generic Objective-C class cannot access the class's generic parameters at runtime

I have no idea on how to fix this. Any help is much appreciated!

like image 550
rgoncalv Avatar asked Sep 29 '16 20:09

rgoncalv


1 Answers

Problem was reported here: https://bugs.swift.org/browse/SR-1576

But in the end you can't use for in with PHFetchResult in Swift 3.0. Let's see some examples:

let collections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
let collectionLists = PHCollectionList.fetchCollectionLists(with: .momentList, subtype: .momentListYear, options: nil)
let assets = PHAsset.fetchAssets(with: .image, options: nil)

You can either use the built-in enumeration of PHFetchResult (my recommended solution):

collections.enumerateObjects(_:) { (collection, count, stop) in
    //...
}
collectionLists.enumerateObjects(_:) { (collectionList, count, stop) in
    //...
}
assets.enumerateObjects(_:) { (asset, count, stop) in
    //...
}

Or access each object by its index:

for idx in 0 ..< collections.count {
    let collection = collections[idx]
    // ...
}
for idx in 0 ..< collectionLists.count {
    let collectionList = collectionLists[idx]
    // ...
}
for idx in 0 ..< assets.count {
    let asset = assets[idx]
    // ...
}
like image 67
Cœur Avatar answered Sep 28 '22 16:09

Cœur