Anthony Dito Anthony Dito - 2 months ago 11
Swift Question

Cannot call value of non-function type '((AnyObject) -> AnyObject?)!' - Swift 3

I am trying to convert my current project to Swift 3 and am running into problems regarding the

objectForKey
to
objectFor
conversion.

The type of myData is
AnyObject


Before:

let x = myData.objectForKey('myKey')


Now:

let x = myData.objectFor('myKey') not working.


In the second case, XCode gives me error
Cannot call value of non-function type '((AnyObject) -> AnyObject?)!'
which I have no idea how to fix.

Does anyone know how to updated the
objectForKey
method and why this conversion is not working?

Other Example



I created another example in a playground to reproduce this issue. The lines of code used are pasted below. The outcome of running this is the same error as stated above.

var x: AnyObject = ["Hello" : "Goodbye"] as AnyObject
x.objectFor("Hello")

Answer

The syntax has changed from:

let x = myData.objectForKey("myKey")

to in Swift 3:

let x = myData.object(forKey:"myKey")

Ex:

var x : AnyObject = ["Hello" : "Goodbye"] as AnyObject
x.object(forKey:"Hello")
Comments