简体   繁体   English

投射为? Swift中的Int”对于不同的数字产生不同的结果

[英]Casting “as? Int” in Swift yields different results for different numbers

I have an iOS app that worked fine, until at some point it started crashing on a nil value for a certain number. 我有一个运行良好的iOS应用程序,直到某个时候它在某个特定值的nil值上开始崩溃。

After doing some tests in my code I managed to boil it down to the following piece of code that yields an output that does not make sense to me at all: 在我的代码中进行了一些测试之后,我设法将其简化为以下代码,这些代码产生的输出对我完全没有意义:

let tc: AnyObject? = jsonObject["times_completed"]
let pos: AnyObject? = jsonObject["position"]

println("TC FROM JSON \(tc)")
println("POS FROM JSON \(pos)")

println("TC TYPE: \(_stdlib_getDemangledTypeName(tc))")
println("POS TYPE: \(_stdlib_getDemangledTypeName(pos))")

let tcInt = tc as? Int
let posInt = pos as? Int

println("TC TYPE: \(_stdlib_getDemangledTypeName(tcInt))")
println("POS TYPE: \(_stdlib_getDemangledTypeName(posInt))")

println("TC: \(tcInt)")
println("POS: \(posInt)")

Output: 输出:

TC FROM JSON Optional(648)
POS FROM JSON Optional(6)

TC TYPE: Swift.Optional<Swift.AnyObject>
POS TYPE: Swift.Optional<Swift.AnyObject>

TC TYPE: Swift.Optional<Swift.Int>
POS TYPE: Swift.Optional<Swift.Int>

TC: nil
POS: Optional(6)

As you can see we have two Int s here, from which one ends up being nil (TC) and the other one end up holding the expected value (POS). 如您所见,我们这里有两个Int ,其中一个最终为nil (TC),另一个最终保持了期望值(POS)。

Since the code has always worked, I suspect that the number itself may be a problem (too high?). 由于代码始终有效,因此我怀疑数字本身可能是个问题(太高了吗?)。

Casting the Int in a different way helps, like so: 以不同的方式投射Int会有所帮助,例如:

let tcInt = tc?.integerValue

But that would mean I have to resubmit my app, which will take some time. 但这意味着我必须重新提交我的应用程序,这将需要一些时间。 I would like to avoid that, maybe by (temporarily) changing the values for times_completed 我想避免这种情况,也许是通过(临时)更改times_completed的值

Can anybody explain what happens here? 有人可以解释这里发生的事情吗? Thanks. 谢谢。


UPDATE: 更新:

Please also check a piece of the JSON below. 另请检查以下JSON。 It seems that it is not a string, but a number. 似乎它不是字符串,而是数字。 The key is a string, but would that matter? 关键一个字符串,但这有关系吗?

...
position = 6;
slug = "my-quiz";
"times_completed" = 648;
...

It seems that your jsonObject["times_completed"] is a string and not a number. 看来您的jsonObject["times_completed"]是一个字符串,而不是数字。

And because .integerValue works for you proves it because its an NSString property. 因为.integerValue为您工作,所以证明了它是因为它具有NSString属性。

And casting a String to an Int will crash. 并且将String转换为Int将会崩溃。

You can also do it like this: 您也可以这样:

let tcInt = (jsonObject["times_completed"] as? String)?.toInt()

I think that jsonObject["times_completed"] is returning a String value and treating String like an Int in let tcInt = tc as? Int 我认为jsonObject["times_completed"]返回一个String值,并在let tcInt = tc as? Int String像Int一样对待let tcInt = tc as? Int let tcInt = tc as? Int is causing a problem. let tcInt = tc as? Int引起问题。 Note that 注意

var a : AnyObject? = 10
var b : AnyObject? = "10"

println(a)
println(b)

prints Optional(10) in both cases. 在两种情况下均输出Optional(10)。 Try casting it as String and check the results let tcInt = tc as? String 尝试将其强制转换为String并检查结果, let tcInt = tc as? String let tcInt = tc as? String . let tcInt = tc as? String If thats correct don't worry, your app isn't crashing because of your fault and maybe something in backend changed 如果正确,请放心,您的应用不会因为您的错误而崩溃,并且后端中的某些内容可能已更改

The result of nil is probably because jsonObject["times_completed"] can not be cast to Int. nil的结果可能是因为jsonObject["times_completed"]无法转换为Int。 Try to cast it to String or other Type. 尝试将其强制转换为String或其他Type。

if let tcStr = jsonObject["times_completed"] as? String {
    // string...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM