[英]cannot convert value of '()' to expected argument type 'String' swift 3.0
I can't find anything online or on stackoverflow. 我无法在线或在stackoverflow上找到任何东西。 I'm getting this error Cannot convert value of '()' to expected argument type 'String'
when I use the let color. 我收到此错误当我使用let颜色时, Cannot convert value of '()' to expected argument type 'String'
。 I'm presuming I need to convert () to a String. 我假设我需要将convert()转换为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",
]
I would assume no0
and no1
are String variables elsewhere. 我会假设no0
和no1
是其他地方的String变量。
Unfortunately in Swift, the assignment is not a statement , but an expression that returns Void
(ie ()
) . 不幸的是,在Swift中, 赋值不是一个语句 ,而是一个返回Void
(即()
)的表达式 。 Thus the compiler won't complain about this declaration, even it certainly looks wrong to the human eye. 因此编译器不会抱怨这个声明,即使它对人眼来说肯定是错误的。
The expression no0 = "7A9474"
can be viewed as a function that returns ()
. 表达式no0 = "7A9474"
可以看作是返回()
的函数。 So the compiler will see an array of two ()
and infer the type of colors
to be [()]
. 因此编译器将看到一个包含两个()
的数组,并推断出colors
的类型为[()]
。
let color = colors[random]
And thus the type of color
is ()
. 因此color
的类型是()
。 (Swift 3 will issue a warning on this line) (Swift 3将在此行发出警告)
Change2(hex: color, number: String(random))
// ^^^^^
And thus a type error on this line. 因此这条线上的类型错误。
Perhaps you want this instead: 也许你想要这个:
no0 = "7A9474"
no1 = "8C4482"
let colors = [no0, no1]
no0,no1...those index is already in array. no0,no1 ......那些索引已经在数组中了。
var current = 0
func Change() {
print("CALL")
let colors = ["7A9474","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))
}
func Change2(hex:String , number:String) {
//do ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.