【问题标题】:Value of type 'EnumeratedSequence<[CGPoint]>' has no member 'compactMap''EnumeratedSequence<[CGPoint]>' 类型的值没有成员 'compactMap'
【发布时间】:2018-04-25 14:15:05
【问题描述】:

我想在我的项目中实现图表,但是当我打开演示项目时出现此错误 Value of type 'EnumeratedSequence' has no member 'compactMap' 我看到了这个Value of type 'CGPoint' has no member 'makeWithDictionaryRepresentation' in swift 3 链接,但错误不是解决了。​​

【问题讨论】:

  • 您使用的是哪个 Xcode 版本? compactMap 是在 Swift 4.1 (Xcode 9.3) 中引入的
  • 我正在使用 swift 4.0 (Xcode 9.2)
  • 是的;没有compactMap 那里。它叫flatMap
  • 所以我使用 flatMap?
  • 完全正确;直到你升级到 Swift 4.1

标签: ios swift


【解决方案1】:

在 Swift 4.0 及更早版本中,Sequence 协议有两个版本的flatMap

Sequence.flatMap<S>(_: (Element) -> S) -> [S.Element] where S : Sequence
Sequence.flatMap<U>(_: (Element) -> U?) -> [U]

在 Swift 4.1 中,SE-0187 将第二个版本重命名为 compactMap

Sequence.flatMap<S>(_: (Element) -> S) -> [S.Element] where S : Sequence
Sequence.compactMap<U>(_: (Element) -> U?) -> [U]

您使用的是已更新到 Swift 4.1 的 Charts 版本,但您使用的是 Swift 4.0 编译器。

你可以:

  1. 降级到仅使用 Swift 4.0 的旧版 Charts。

  2. 升级到支持 Swift 4.1 的 Xcode 9.3。

  3. 将您的 Charts 副本更改为使用 flatMap 而不是 compactMap

  4. 在您的 Charts 副本中添加一个“垫片”以添加 compactMap(感谢 BasThomas):

    #if swift(>=4.1)
    #else
    extension Collection {
      func compactMap<ElementOfResult>(
        _ transform: (Element) throws -> ElementOfResult?
      ) rethrows -> [ElementOfResult] {
        return try flatMap(transform)
      }
    }
    #endif
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多