Type "Int32" does not comply with the protocol "AnyObject" Swift?

I have a subclass of Model, a subclass of NSObject , as shown 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 downloaded NSMutableData and made JSON out of it using NSJSONSerialization .

My code

 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 an error

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

where i used Int32 .

Image below

enter image description here

thanks

+5
source share
1 answer

Int32 cannot automatically connect to Objective-C NSNumber .

See this document :

All of the following types automatically connect to NSNumber:

  • Int
  • UInt
  • Float
  • Twice
  • Bool

So you need to do the following:

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

By the way, why are you using Int32 ? Unless you have any specific reason, you should use Int .

+13
source

Source: https://habr.com/ru/post/1207109/


All Articles