2014-11-18 10 views
5

Ich habe ein Modell, Unterklasse von NSObject, sieht wie folgt aus.Typ 'Int32' entspricht nicht dem Protokoll '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() 
} 

Ich habe Download NSMutableData und machte JSON daraus NSJSONSerialization verwenden.

mein 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 

    } 

ich Fehler

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

wo ich Int32 verwendet.

Bild unten

enter image description here

Dank

Antwort

13

Int32 kann nicht automatisch von Objective-C NSNumber überbrückt werden.

Siehe this document:

Alle folgenden Typen sind NSNumber automatisch überbrückt:

  • Int
  • UInt
  • Float
  • Doppel
  • Bool

So haben Sie wie dies zu tun:

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

BTW, warum Sie Int32 verwenden? Wenn Sie keinen bestimmten Grund haben, you should use Int.

+3

Ich benutze als meine Datenbank haben den gleichen Datentyp, dh Int32 und Int16 usw. –

+0

Kann auch verwenden: '... = (json [" CategoriesVer "] als! NSNumber) .intValue' (oder andere NSNumber Accessor) . –

Verwandte Themen