类型'Int32'不符合协议'AnyObject'Swift? [英] Type 'Int32' does not conform to protocol 'AnyObject' Swift?
本文介绍了类型'Int32'不符合协议'AnyObject'Swift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 NSObject
的Model,子类,如下所示。
I have a Model, subclass of NSObject
, looks like as below.
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
并从中获取 JSON
使用 NSJSONSerialization
。
I have download NSMutableData
and made JSON
from it using 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
}
我收到错误
Type '`Int32`' does not conform to protocol 'AnyObject'
我使用 Int32
。
想象一下e
谢谢
推荐答案
Int32
无法自动从Objective-C桥接 NSNumber
。
Int32
cannot be automatically bridged from Objective-C NSNumber
.
参见这个文件:
所有以下类型都自动桥接到NSNumber:
All of the following types are automatically bridged to NSNumber:
- Int
- UInt
- Float
- Double
- Bool
- Int
- UInt
- Float
- Double
- Bool
所以你必须这样做:
configDao.categoriesVer = Int32(json["CategoriesVer"] as Int)
BTW,为什么你使用 Int32
?如果您没有任何具体原因,你应该使用 Int
。
BTW, why you use Int32
? If you don't have any specific reason, you should use Int
.
这篇关于类型'Int32'不符合协议'AnyObject'Swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文