相关疑难解决方法(0)

奇怪的斯威夫特数字类型铸造

我刚刚注意到Swift对Int和Double进行了一些类型的转换.当我尝试评估时

(10 / 3.0) - (10 / 3)
Run Code Online (Sandbox Code Playgroud)

0.333...预计,但实际上0.0.有人可以解释一下吗?

casting type-inference swift

12
推荐指数
1
解决办法
594
查看次数

Swift运算符"*"在两个Ints上抛出错误

我在这里有一个非常奇怪的错误,我四处搜索,我已经尝试了所有的建议.没有工作.

scrollView.contentSize.height = 325 * globals.defaults.integer(forKey: "numCards")
Run Code Online (Sandbox Code Playgroud)

二进制运算符'*'不能应用于两个'Int'操作数

WTF斯威夫特!为什么不?我Ints一直在增加.这些ARE 2 Ints. globals.defaults只是一个例子UserDefaults.standard.我每次都尝试以下相同的错误.

325 * Int(globals.defaults.integer(forKey: "numCards")   //NOPE

Int(325) * Int(globals.defaults.integer(forKey: "numCards"))  //NOPE

if let h = globals.defaults.integer(forKey: "numCards"){
    325 * h  //NOPE, and 'Initializer for conditional binding must have optional type, not Int'
}

let h = globals.defaults.integer(forKey: "numCards") as! Int
325 * h //NOPE, and 'Forced cast of Int of same type as no affect'

325 * 2 //YES!  But no shit... …
Run Code Online (Sandbox Code Playgroud)

swift swift3

11
推荐指数
1
解决办法
4603
查看次数

标签 统计

swift ×2

casting ×1

swift3 ×1

type-inference ×1