我在网上或 stackoverflow 上找不到任何东西。
当我使用 let 颜色时,出现此错误 Cannot convert value of '()' to expected argument type 'String'
。我假设我需要将 () 转换为字符串。
func Change() {
print("CALL")
let colors = [
no0 = "7A9474",
no1 = "8C4482",
]
let random = Int(arc4random_uniform(UInt32(colors.count)))
let color = colors[random]
if random == current {
print("FUNNY")
Change()
}
current = random
Change2(hex: color, number: String(random)) //HERE IS THE ERROR
}
最佳答案
let colors = [
no0 = "7A9474",
no1 = "8C4482",
]
我假设 no0
和 no1
是别处的字符串变量。
不幸的是,在 Swift 中,assignment is not a statement, but an expression that returns Void
(i.e. ()
) .因此编译器不会提示这个声明,即使它在人眼中肯定看起来是错误的。
表达式 no0 = "7A9474"
可以看作是一个返回 ()
的函数。因此,编译器将看到一个包含两个 ()
的数组,并将 colors
的类型推断为 [()]
。
let color = colors[random]
因此color
的类型是()
。 (Swift 3 会在这一行发出警告)
Change2(hex: color, number: String(random))
// ^^^^^
因此这一行出现类型错误。
也许你想要这个:
no0 = "7A9474"
no1 = "8C4482"
let colors = [no0, no1]
关于ios - 无法将 '()' 的值转换为预期的参数类型 'String' swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803537/