简体   繁体   English

无法将'()'的值转换为预期的参数类型'String'swift 3.0

[英]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. 我会假设no0no1是其他地方的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.

相关问题 Swift 3.0无法将(_,_)->()类型的值转换为期望的参数类型'ObjectsOrErrorBlock' - Swift 3.0 cannot convert value of type (_, _)->() to Expected argument type 'ObjectsOrErrorBlock' 无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift - Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift ios无法将类型“()”的值转换为预期的参数类型“字符串”,迅速3 - ios Cannot convert value of type '()' to expected argument type 'String' swift 3 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 Swift 1.2到Swift 2:无法将类型的值转换为预期的参数类型 - Swift 1.2 to swift 2: Cannot convert value of type to expected argument type Swift 3.0迁移后的Alamofire错误:“无法将类型'(URL,HTTPURLResponse) - >(URL)'的值转换为预期的参数类型'参数'”? - Alamofire error after Swift 3.0 migration: “Cannot convert value of type '(URL, HTTPURLResponse)-> (URL)' to expected argument type 'Parameters'”? 无法将类型'String?.Type'的值转换为预期的参数类型'String?' - Cannot convert value of type 'String?.Type' to expected argument type 'String?' swift无法将类型(_,_)-> _的值转换为预期的参数类型'((_,CGFloat))-> _ - swift cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _ swift:无法将类型“()”的值转换为预期的参数类型“ [Double]” - swift : Cannot convert value of type '()' to expected argument type '[Double]' Swift 无法将类型“()”的值转换为预期的参数类型“() -> Void” - Swift Cannot convert value of type '()' to expected argument type '() -> Void'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM