【问题标题】:IBanimitable issue in xcode 9?xcode 9 中的 IBanimitable 问题?
【发布时间】:2017-10-24 06:57:34
【问题描述】:

我正在将 cocoapod 用于 ibanimatable。我之前将它与 xcode 一起使用,它工作正常。但现在我更新 pod 时它开始给我错误。

public extension PlaceholderDesignable where Self: UITextField {

  var placeholderText: String? { get { return "" } set {} }

  public func configurePlaceholderColor() {
    let text = placeholder ?? placeholderText
    if let placeholderColor = placeholderColor, let placeholder = text {
      attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [.foregroundColor: placeholderColor])
    }
  }
}

下面的行给了我错误

 attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [.foregroundColor: placeholderColor])

类型“字符串”没有成员“前景颜色”

请告诉我如何才能摆脱这个问题?

【问题讨论】:

    标签: ios swift xcode ios11 xcode9


    【解决方案1】:

    根据here,属性键不是在String中声明的,而是在NSAttributedStringKey中声明的。这意味着您不能使用方便的“只需在此处放置一个点,编译器就会知道您的意思”的语法糖。键是字符串,所以编译器认为你的意思是String.foregroundColor,但显然这样的东西不存在。

    要解决此问题,请正确写入类型名称:

    NSAttributedString(string: placeholder, attributes: [NSAttributedStringKey.foregroundColor: placeholderColor])
    

    【讨论】:

    • 是最新的 swift 问题吗?因为我使用的是 ibanimitable 最新版本
    • “快速问题”是什么意思?如果您的意思是“编译器错误”,那么不,这是预期的行为。如果您的意思是“您的 swift 代码有问题”,是的,在这种特定情况下,您不能只省略类型名称。 @DhirajKumar
    • 我这么说是因为这段代码被一个非常流行的 ios 库 IBANimitable 使用。
    • IBAnimatable无关。您只是错误地使用了NSAttributedString。 @DhirajKumar
    • 好的,但我这样说是因为这段代码在 PlaceholderDesignable 中。 PlaceholderDesignable 是 IBAnimitable 下的协议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2016-01-13
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多