Type 'Int32' does not conform to protocol 'AnyObject' Swift?

核能气质少年 提交于 2019-12-07 07:25:17

问题


I have a Model, subclass of NSObject, looks like as below.

class ConfigDao: NSObject {
    var categoriesVer : Int32 = Int32()
    var fireBallIP : String =  String ()
    var fireBallPort : Int32 = Int32()
    var isAppManagerAvailable : Bool = Bool()
    var timePerQuestion : String = String ()
    var isFireballAvailable : Bool = Bool ()
}

I have download NSMutableData and made JSON from it using NSJSONSerialization.

My Code is

func parserConfigData (data :NSMutableData) -> ConfigDao{

        var error : NSError?
        var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary

        var configDao : ConfigDao = ConfigDao()

        println("Print Config \(json)")

        configDao.categoriesVer = json["CategoriesVer"] as Int32
        configDao.fireBallIP = json["FireBallIP"] as String
        configDao.fireBallPort = json["FireBallPort"] as Int32
        configDao.isAppManagerAvailable = json["IsAppManagerAvailable"] as Bool
        configDao.timePerQuestion = json["TimePerQuestion"] as String
        configDao.isFireballAvailable = json["IsFireballAvailable"] as Bool

        return configDao

    }

I get error

Type '`Int32`' does not conform  to protocol 'AnyObject' 

where I used Int32.

Image Below

Thanks


回答1:


Int32 cannot be automatically bridged from Objective-C NSNumber.

See this document:

All of the following types are automatically bridged to NSNumber:

  • Int
  • UInt
  • Float
  • Double
  • Bool

So you have to do like this:

configDao.categoriesVer = Int32(json["CategoriesVer"] as Int)

BTW, why you use Int32? If you don't have any specific reason, you should use Int.



来源:https://stackoverflow.com/questions/26991246/type-int32-does-not-conform-to-protocol-anyobject-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!