簡體   English   中英

無法將'()'的值轉換為預期的參數類型'String'swift 3.0

[英]cannot convert value of '()' to expected argument type 'String' swift 3.0

我無法在線或在stackoverflow上找到任何東西。 我收到此錯誤當我使用let顏色時, Cannot convert value of '()' to expected argument type '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",
]

我會假設no0no1是其他地方的String變量。

不幸的是,在Swift中, 賦值不是一個語句 ,而是一個返回Void (即() )的表達式 因此編譯器不會抱怨這個聲明,即使它對人眼來說肯定是錯誤的。

表達式no0 = "7A9474"可以看作是返回()的函數。 因此編譯器將看到一個包含兩個()的數組,並推斷出colors的類型為[()]

let color = colors[random]

因此color的類型是() (Swift 3將在此行發出警告)

Change2(hex: color, number: String(random))
//           ^^^^^

因此這條線上的類型錯誤。


也許你想要這個:

no0 = "7A9474"
no1 = "8C4482"
let colors = [no0, no1]

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM