Search code examples
iosswiftstringnsmutabledictionary

Value of type 'NSMutableDictionary' has no member 'string' in swift


I am new in swift and I am getting error in xcode 12.3. Before it was working fine

my code is like this

let dictOption = options[0] as! NSMutableDictionary
 btn2.setTitle(dictOption.string(forKey: "optionValue"), for: .normal)

but it is showing error

Value of type 'NSMutableDictionary' has no member 'string'

did anyone face the similar issue.

Please help!


Solution

  • Replace

    dictOption.string(forKey: "optionValue")
    

    with

    dictOption.value(forKey: "optionValue")
    

    Or more Swifty

    guard let dic = options.first as? [String:Any] , 
          let value = dic["optionValue"] as? String else { return }
    btn2.setTitle(value, for: .normal)