How to convert AnyObject type to Int in Swift

后端 未结 3 636
野性不改
野性不改 2021-02-06 03:47

I am searching a key in an Array of Dictionarys and I want to convert the result value into an Int value. This is what I tried.

         


        
3条回答
  •  再見小時候
    2021-02-06 04:25

    Here i have given example to convert Anyobject to Int and String

    var idInt : Int = 0
    if let ids: AnyObject = responseDict["id"] {
        if let idsInt = ids as? Int{
            idInt  = idsInt
            print(idInt)
        }
    }
    
    
    var nameString : String = ""
        if let name: AnyObject = responseDict["name"] {
            if let nameStr = name as? String{
                nameString  = nameStr
                print(nameString)
            }
        }
    

提交回复
热议问题