2014-11-18 30 views
5

Ho un modello, sottoclasse di NSObject, è come mostrato di seguito.Il tipo 'Int32' non è conforme al protocollo 'AnyObject' Swift?

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() 
} 

devo scaricare NSMutableData e reso JSON da esso usando 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 

    } 

ottengo errore

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

in cui ho usato Int32.

immagine sotto

enter image description here

Grazie

risposta

13

Int32 non può essere colmato automaticamente da Objective-C NSNumber.

Vedi this document:

Tutti i seguenti tipi sono ponticellati automaticamente NSNumber:

  • Int
  • UInt
  • Float
  • doppio
  • Bool

Quindi bisogna fare in questo modo:

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

BTW, il motivo per cui si utilizza Int32? Se non si dispone di alcun motivo specifico, you should use Int.

+3

Sto usando come il mio database ha lo stesso tipo di dati, cioè Int32 e Int16 ecc. –

+0

Inoltre può utilizzare: '... = (json [" CategorieVer "] come! NSNumber) .intValue' (o qualsiasi altro accessore NSNumber) . –

Problemi correlati