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

标签 ios swift swift3

我在网上或 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",
]

我假设 no0no1 是别处的字符串变量。

不幸的是,在 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/

相关文章:

ios - 如何根据其条形调整 x 轴标签?

ios - 更新 iOS 应用程序时,Documents 文件夹内容会发生什么变化?

iphone - 在非正式协议(protocol)中委托(delegate)变量

ios - 使用未声明的类型 'sqlite3_stmt'

ios - 获取 slider 的当前时间并播放歌曲

ios - 显示事件的容器 View

ios - Kudan:是否可以在检测到标记时调用函数?

ios - NSAttributedString 行间距打破 <uk> 布局

swift - 如何在 SwiftUI 列表中重新加载数据?

swift3 - 如何在 swift 3.0 中比较日期?