【问题标题】:Type 'Int32' does not conform to protocol 'AnyObject' Swift?类型“Int32”不符合协议“AnyObject”Swift?
【发布时间】:2014-11-18 09:52:07
【问题描述】:

我有一个模型,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 ()
}

我已经下载了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

    }

我得到错误

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

我在哪里使用Int32

下图

谢谢

【问题讨论】:

    标签: ios iphone swift int32


    【解决方案1】:

    Int32 不能自动从 Objective-C NSNumber 桥接。

    this document:

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

    • 内部
    • UInt
    • 浮动
    • 布尔

    所以你必须这样做:

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

    顺便说一句,你为什么使用Int32?如果没有具体原因,you should use Int

    【讨论】:

    • 我使用的数据库具有相同的数据类型,即 Int32 和 Int16 等。
    • 也可以使用:... = (json["CategoriesVer"] as! NSNumber).intValue(或任何其他 NSNumber 访问器)。
    猜你喜欢
    • 2015-09-12
    • 2014-10-23
    • 2015-01-02
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多