Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - PHFetchResult - enumerateObjects - Ambiguous use of 'enumerate objects'

Tags:

swift

swift3

Not so sure why I'm getting "Ambiguous use of 'enumerate objects' in Swift 3.

let collections = PHAssetCollection.fetchAssetCollections(with: .moment, subtype: .any, options: nil)

    collections.enumerateObjects { (collection, start, stop) in
        collection as! PHAssetCollection
        let assets = PHAsset.fetchAssets(in: collection, options: nil)
        assets.enumerateObjects({ (object, count, stop) in
            content.append(object)
        })

    }

Any thoughts? This code was working fine in Swift 2.2

like image 985
Ron Buencamino Avatar asked Sep 05 '16 17:09

Ron Buencamino


2 Answers

I have run into this a few times myself, and it appears to be an issue with Swift's trailing closure syntax. Including parentheses around the closure argument should do the trick:

collections.enumerateObjects({ (collection, start, stop) in
    collection as! PHAssetCollection
    let assets = PHAsset.fetchAssets(in: collection, options: nil)
    assets.enumerateObjects({ (object, count, stop) in
        content.append(object)
    })

})

Edit: Please see rintaro's answer for an explanation of why this happens.

like image 158
Matthew Seaman Avatar answered Nov 20 '22 06:11

Matthew Seaman


When using enumerateObjects with trailing closure, there are two overloaded candidates for it:

  • enumerateObjects(_:)
  • enumerateObjects(options:using:)(because options: is defaulted)

Currently, we need to disambiguate this.

If you want to use trailing closure syntax, here's the workaround:

assets.enumerateObjects(_:) { (object, count, stop) in
    content.append(object)
}

This works because this is equivalent to

let unapplied = assets.enumerateObjects(_:)
unapplied { (object, count, stop) in
    content.append(object)
}
like image 42
rintaro Avatar answered Nov 20 '22 06:11

rintaro