1

I am currently facing this problem (Value type of "Any" has no member 'objectforKey') due to swift 3 upgrade. Anybody knows why?

Here is my line of code that have the error

            let bookName:String  = (accounts[indexPath.row] as AnyObject).objectForKey("bookName") as! String

*accounts is an array.

LuidonVon
  • 443
  • 6
  • 20

2 Answers2

5

Okay basically it is the .objectForKey needs to be change as the following:

    let bookName:String  = (accounts[indexPath.row] as AnyObject).object(forKey:"bookName") as! String
LuidonVon
  • 443
  • 6
  • 20
2

As always, do not use NS(Mutable)Array / NS(Mutable)Dictionary in Swift. Both types lack type information and return Any which is the most unspecified type in Swift.

Declare accounts as Swift Array

var accounts = [[String:Any]]()

Then you can write

let bookName = accounts[indexPath.row]["bookName"] as! String

Another Dont: Do not annotate types that the compiler can infer.

vadian
  • 274,689
  • 30
  • 353
  • 361