Swift AnyObject is not convertible to String/Int

后端 未结 4 1035
天命终不由人
天命终不由人 2020-12-29 19:42

I want to parse a JSON to object, but I have no idea how to cast AnyObject to String or Int since I\'m getting:

0x106bf1d07:  leaq   0x33130(%rip), %rax              


        
4条回答
  •  灰色年华
    2020-12-29 20:29

    reminderJSON["id"] gives you an AnyObject?, so you cannot cast it to Int You have to unwrap it first.

    Do

    self.id = reminderJSON["id"]! as Int
    

    if you're sure that id will be present in the JSON.

    if id: AnyObject = reminderJSON["id"] {
        self.id = id as Int
    }
    

    otherwise

提交回复
热议问题