ios - 收到错误 : Type 'String' has no member 'foregroundColor' in Swift 4

标签 ios swift

我是 Swift 的新手。我在 Swift 4 中创建了一个应用程序,但是当我将 SWIFT_VERSION 更改为 Swift 3.0 时,我的代码出现错误。

Type 'String' has no member 'foregroundColor'.

如何将其转换为当前的 Swift 语法?

代码:

    if let p = placeholder {

      let place = NSAttributedString(string: p, attributes: //error -->[.foregroundColor: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])


        attributedPlaceholder = place

        textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

最佳答案

在 Swift 4 之前,带属性的字符串属性不是枚举。在这种情况下,您应该使用 NSForegroundColorAttributeName

NSAttributedString(string: p, attributes: [NSForegroundColorAttributeName: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])

关于ios - 收到错误 : Type 'String' has no member 'foregroundColor' in Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45422048/

相关文章:

swift - UITabBarController 始终在 UIViewController 顶部打开页面

iphone - 隐藏有关缺少体系结构信息的 Xcode dsymutil 警告

ios - 如果UITableViewCell有多个按钮,那么在哪里处理触摸事件是最好的

ios - 核心蓝牙的问题

ios - 有没有办法在 Interface Builder 的自动布局约束中添加标识符?

ios - Swift:核心动画没有动画

ios - 在 iOS 5.0 模拟器中使用 UIManagedDocument 时出现 NSFileCoordinator 错误

ios - 在新的 Swift View Controller 文件上添加按钮不起作用

swift - 如何将枚举大小写传递到使用 'if case' 检查属性大小写的函数中

ios - 如何更改 SceneKit 中枢轴点的位置