Swift NSNumber作为可选项,即使它未声明为一
我在swift字典里储存了一个号码。我使用的是NSNumber,因为我必须序列化为JSON。我不明白为什么下面的第五行不行,第六行不行。Swift NSNumber作为可选项,即使它未声明为一,swift,Swift,我在swift字典里储存了一个号码。我使用的是NSNumber,因为我必须序列化为JSON。我不明白为什么下面的第五行不行,第六行不行。currentCt似乎认为它是可选的,但是,在Dictionary()的第1行,它没有被声明为可选的a有什么想法吗 另外,我不知道为什么桥接不允许自动添加Int和NSNumber,我必须使用Int(currentCt!)。同样,任何想法都是非常感谢的 var activeMinDic = Dictionary<String, NSNumber>()
currentCt
似乎认为它是可选的,但是,在Dictionary()的第1行,它没有被声明为可选的a
有什么想法吗
另外,我不知道为什么桥接不允许自动添加Int和NSNumber,我必须使用Int(currentCt!)。同样,任何想法都是非常感谢的
var activeMinDic = Dictionary<String, NSNumber>()
activeMinDic["1"] = 5
var currentCt = activeMinDic[String(1)]
activeMinDic[String(1)] = 1 + 1 // works fine
activeMinDic[String(1)] = 1 + currentCt // does not work
activeMinDic[String(1)] = 1 + Int(currentCt!) // works
var activeMinDic=Dictionary()
activeMinDic[“1”]=5
var currentCt=activeMinDic[String(1)]
activeMinDic[String(1)]=1+1//工作正常
activeMinDic[String(1)]=1+currentCt//不工作
activeMinDic[String(1)]=1+Int(currentCt!)//有效
您应该将字典声明为[Int:NSNumber],这样您就可以使用Int作为键,并使用if-let打开字典值。看一看:
var activeMinDic:[Int: NSNumber] = [:]
activeMinDic[1] = 5
if let currentCt = activeMinDic[1] as? Int {
println(currentCt)
activeMinDic[currentCt] = 1 + 1 // works fine
activeMinDic[currentCt] = 1 + currentCt
}
您应该将字典声明为[Int:NSNumber],这样您就可以使用Int作为键,并使用if-let打开字典值。看一看:
var activeMinDic:[Int: NSNumber] = [:]
activeMinDic[1] = 5
if let currentCt = activeMinDic[1] as? Int {
println(currentCt)
activeMinDic[currentCt] = 1 + 1 // works fine
activeMinDic[currentCt] = 1 + currentCt
}
您应该将字典声明为[Int:NSNumber],这样您就可以使用Int作为键,并使用if-let打开字典值。看一看:
var activeMinDic:[Int: NSNumber] = [:]
activeMinDic[1] = 5
if let currentCt = activeMinDic[1] as? Int {
println(currentCt)
activeMinDic[currentCt] = 1 + 1 // works fine
activeMinDic[currentCt] = 1 + currentCt
}
您应该将字典声明为[Int:NSNumber],这样您就可以使用Int作为键,并使用if-let打开字典值。看一看:
var activeMinDic:[Int: NSNumber] = [:]
activeMinDic[1] = 5
if let currentCt = activeMinDic[1] as? Int {
println(currentCt)
activeMinDic[currentCt] = 1 + 1 // works fine
activeMinDic[currentCt] = 1 + currentCt
}
您遇到的问题并非特定于
NSNumber
。Swift字典返回一个可选的,因为它们的键可能不在字典中
所以这一行:
var currentCt = activeMinDic[String(1)]
是这方面的简写:
var currentCt: NSNumber? = activeMinDic[String(1)]
您必须以某种方式打开键获取的返回值,例如:
// default to an NSNumber of 0 if not present
let currentCt = activeMinDic[String(1)] ?? 0
// or require it:
if let currentCt = activeMinDic[String(1)] {
// use currentCt
}
else {
// handle currentCt not being present
}
重新桥接–没有从
NSNumber
到其他数字类型的隐式转换。事实上,对于两个nsnumber
,甚至没有一个+
。请记住,NSNumber
可以包含各种不同类型的数字表示,您可以使用.floatValue
、.integerValue
等进行提取。Swift作为一种语言,在不同类型之间不隐式转换方面非常严格(与其他一些类似C的语言不同)(避免隐式截断等所有相关问题),因此您需要显式地声明要提取的数值(使用.integerValue
,作为Int
,或使用Int
的init
)
所以这一行:
var currentCt = activeMinDic[String(1)]
是这方面的简写:
var currentCt: NSNumber? = activeMinDic[String(1)]
您必须以某种方式打开键获取的返回值,例如:
// default to an NSNumber of 0 if not present
let currentCt = activeMinDic[String(1)] ?? 0
// or require it:
if let currentCt = activeMinDic[String(1)] {
// use currentCt
}
else {
// handle currentCt not being present
}
重新桥接–没有从
NSNumber
到其他数字类型的隐式转换。事实上,对于两个NSNumber
,甚至没有一个+
。请记住,NSNumber
可以包含各种不同类型的数字表示,您可以在中使用.floatValue
,TEGRVALUE
等。Swift作为一种语言,在不同类型之间不进行隐式转换(避免隐式截断等相关问题)方面非常严格(与其他一些类似C的语言不同),因此您需要显式声明要提取的数值也就不足为奇了(使用.integerValue
,作为Int
,或者使用Int
的init
。您遇到的问题不是特定于NSNumber
。而是Swift字典返回一个可选项,因为它们的键可能不在字典中
所以这一行:
var currentCt = activeMinDic[String(1)]
是这方面的简写:
var currentCt: NSNumber? = activeMinDic[String(1)]
您必须以某种方式打开键获取的返回值,例如:
// default to an NSNumber of 0 if not present
let currentCt = activeMinDic[String(1)] ?? 0
// or require it:
if let currentCt = activeMinDic[String(1)] {
// use currentCt
}
else {
// handle currentCt not being present
}
重新桥接–没有从
NSNumber
到其他数字类型的隐式转换。事实上,对于两个NSNumber
,甚至没有一个+
。请记住,NSNumber
可以包含各种不同类型的数字表示,您可以在中使用.floatValue
,TEGRVALUE
等。Swift作为一种语言,在不同类型之间不进行隐式转换(避免隐式截断等相关问题)方面非常严格(与其他一些类似C的语言不同),因此您需要显式声明要提取的数值也就不足为奇了(使用.integerValue
,作为Int
,或者使用Int
的init
。您遇到的问题不是特定于NSNumber
。而是Swift字典返回一个可选项,因为它们的键可能不在字典中
所以这一行:
var currentCt = activeMinDic[String(1)]
是这方面的简写:
var currentCt: NSNumber? = activeMinDic[String(1)]
您必须以某种方式打开键获取的返回值,例如:
// default to an NSNumber of 0 if not present
let currentCt = activeMinDic[String(1)] ?? 0
// or require it:
if let currentCt = activeMinDic[String(1)] {
// use currentCt
}
else {
// handle currentCt not being present
}
重新桥接–没有从
NSNumber
到其他数字类型的隐式转换。事实上,对于两个NSNumber
,甚至没有一个+
。请记住,NSNumber
可以包含各种不同类型的数字表示,您可以在中使用.floatValue
,TEGRVALUE
等。Swift作为一种语言,在不同类型之间不进行隐式转换(避免隐式截断等相关问题)方面非常严格(与其他一些类似C的语言不同),因此您需要显式声明要提取的数值也就不足为奇了(使用.integerValue
,作为Int
,或者使用Int
的init
,就像您在这里所做的那样)。字典的类型是[String:NSNumber]