"EnumeratedSequence <[CGPoint]>"类型的值没有成员"compactMap"

Vid*_*wal 2 ios swift

我想在我的项目中实现Charts,但是当我打开demo项目时我得到了这个错误类型'EnumeratedSequence <[CGPoint]>'的值没有成员'compactMap'我看到这个类型'CGPoint'的值没有成员'makeWithDictionaryRepresentation '在swift 3链接但错误未解决.

rob*_*off 7

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

Sequence.flatMap<S>(_: (Element) -> S) -> [S.Element] where S : Sequence
Sequence.flatMap<U>(_: (Element) -> U?) -> [U]
Run Code Online (Sandbox Code Playgroud)

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

Sequence.flatMap<S>(_: (Element) -> S) -> [S.Element] where S : Sequence
Sequence.compactMap<U>(_: (Element) -> U?) -> [U]
Run Code Online (Sandbox Code Playgroud)

您正在使用已更新为Swift 4.1的图表版本,但您使用的是Swift 4.0编译器.

您可以:

  1. 降级到仅使用Swift 4.0的旧版图表.

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

  3. 更改您的图表副本flatMap而不是使用compactMap.

  4. 在您的图表副本中添加"垫片"以添加compactMap(感谢BasThomas):

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