boa_in_samoa
boa_in_samoa

Reputation: 607

Ambiguous use of 'value'

After converting my project to Swift 3, I'm getting this message:

Ambiguous use of 'value'

On the line

let fetcher = wrapper?.value as? Fetcher<UIImage>

I'm trying to associate with NSObject instances using extensions

public extension UIImageView {
   var hnk_fetcher : Fetcher<UIImage>! {
      get {
          let wrapper = objc_getAssociatedObject(self, &HanekeGlobals.UIKit.SetImageFetcherKey) as? ObjectWrapper
          let fetcher = wrapper?.value as? Fetcher<UIImage>  //Ambiguous use of 'value'
          return fetcher
      }
      set (fetcher) {
          var wrapper : ObjectWrapper?
          if let fetcher = fetcher {
              wrapper = ObjectWrapper(value: fetcher)
          }
          objc_setAssociatedObject(self, &HanekeGlobals.UIKit.SetImageFetcherKey, wrapper, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
      }
  }

Here is my ObjectWrapper class.

class ObjectWrapper : NSObject {
    let value: Any

    init(value: Any) {
        self.value = value
    }
}

Everything was fine before migrating the code. What went wrong?

Upvotes: 3

Views: 4879

Answers (1)

Hamish
Hamish

Reputation: 80801

The problem is that the use of .value is ambiguous to Swift, as it could either refer to your value property, or to any of NSObject's value(for...) family of methods for Key-Value Coding.

I don't believe there's an easy way of disambiguating this by just using Swift syntax (given that your value property is typed as Any – which the methods can also be typed as).

Although amusingly, you can actually use Key-Value Coding itself to get the value:

let fetcher = wrapper?.value(forKeyPath: #keyPath(ObjectWrapper.value)) as? Fetcher<UIImage>

But honestly, the easiest solution would be just to rename your value property to something else (base maybe?).

Upvotes: 2

Related Questions