Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String'

前端 未结 2 752
孤城傲影
孤城傲影 2020-12-16 19:50

ERROR:

Cannot subscript a value of type \'[NSObject : AnyObject]?\' with an index of type \'String\'

CODE:

  f         


        
2条回答
  •  伪装坚强ぢ
    2020-12-16 20:22

    I think it is far more clear to use optional binding so that the println is only invoked when there is an actual value to print

    func getApple(appleId: String) {
         if let apples = userDefaults.dictionaryForKey("apples_array") {
            println(apples[appleId])
         }
    }
    

提交回复
热议问题