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/

相关文章:

objective-c - OpenGL ES 在移动背景时崩溃,iOS 5.1

ios - 有没有便捷的方法可以在 Pinterest IOS SDK 中删除 PIN 码?

ios - 如何访问 Objective-C 中的 Swift 扩展?

ios - 运行应用程序时不显示 AdMob 横幅广告

ios - 通过一个ViewController控制多个子ViewController 'root'

ios - 是否可以使用CoreLocation/MapKit查找纬度/经度附近的地址?

ios - 如何实现从 App Store 自动更新 iOS 应用程序?

swift - 使用 Swift 创建一个 NSAlert

swift - UIImageJPEGRepresentation内存问题

ios - 使用 Swift 对日历进行 Google 身份验证