类型'Int32'不符合协议'AnyObject'Swift?

Sam*_*ikh 4 iphone int32 ios swift

我有一个Model,子类NSObject,如下所示.

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 ()
}
Run Code Online (Sandbox Code Playgroud)

我已经下载NSMutableData并使用JSON它制作NSJSONSerialization.

我的代码是

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

    }
Run Code Online (Sandbox Code Playgroud)

我收到错误

Type '`Int32`' does not conform  to protocol 'AnyObject' 
Run Code Online (Sandbox Code Playgroud)

在这里我使用Int32.

下面的图片

在此输入图像描述

谢谢

rin*_*aro 13

Int32无法从Objective-C自动桥接NSNumber.

看到这个文件:

以下所有类型都自动桥接到NSNumber:

  • 诠释
  • UINT
  • 浮动
  • 布尔

所以你必须这样做:

configDao.categoriesVer = Int32(json["CategoriesVer"] as Int)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你为什么用Int32?如果您没有任何具体原因,您应该使用Int.

  • 我用我的数据库有相同的数据类型,即Int32和Int16等. (3认同)