2014-11-18 2 views
5

모델, 하위 클래스는 NSObject입니다. 아래 그림과 같습니다.'Int32'유형이 프로토콜 '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() 
} 

나는 다운로드 NSMutableData을하고 NSJSONSerialization를 사용 JSON했다.

내 코드

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 

    } 

내가 오류를 내가 Int32을 사용

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

를 얻을 수있다.

이미지 덕분에

enter image description here

아래 은

답변

13

Int32가 자동으로 목표 - C NSNumber에서 다리를 할 수 없습니다.

this document 참조 :

는 다음과 같은 유형의

모든 자동의 NSNumber에 브리지됩니다

  • 지능
  • UINT을
  • 플로트
  • 더블
  • BOOL
  • ,536을

그래서 당신은 이런 식으로 할 필요가 :

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

BTW, 왜 Int32 사용? 구체적인 이유가없는 경우 you should use Int.

+3

내 데이터베이스에 Int32 및 Int16 등의 동일한 데이터 유형을 사용하고 있습니다. –

+0

또한 사용할 수 있습니다 : '... = (json [ "CategoriesVer"] as! NSNumber) .intValue' (또는 다른 NSNumber 접근 자) . –

관련 문제