ios - 更新到 Swift 2.0 'map' 不可用 : call the 'map()' method on the sequence

标签 ios xcode swift swift2

代码来自一个名为 ZLBalancedFlowLayout 的 github 控件。 (link)。我一直在尝试更新到 Swift 2.0,但出现此错误“‘map’不可用:在序列上调用‘map()’方法”。任何意见将是有益的。提前谢谢你。

 private func collectionView(collectionView:UICollectionView, framesForItemsInSection section:Int, inout updateContentSize contentSize:CGSize) -> ([CGRect], [CGFloat]) {

    let maxWidth = Float(scrollDirection == .Vertical ? contentSize.width : contentSize.height),
    widths = map(0..<collectionView.numberOfItemsInSection(section), {(item: Int) -> Float in
        let itemSize = self.sizeForItemAtIndexPath(NSIndexPath(forItem: item, inSection: section)),
        ratio = self.scrollDirection == .Vertical ?
            itemSize.width/itemSize.height :
            itemSize.height/itemSize.width
        return min(Float(ratio*self.rowHeight), Float(maxWidth))
    })

最佳答案

在 Swift 2 中,map 不再是全局函数,它是您在序列上 调用的方法。

代替:

let result = map(array, closure)

做:

let result = array.map(closure)

同样的概念也适用于其他序列方法,如enumeratesplit等。

关于ios - 更新到 Swift 2.0 'map' 不可用 : call the 'map()' method on the sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951594/

相关文章:

swift - 如何在运行时预选表行?

ios - SwiftUI · 后台计时器在模拟器上有效,但在真实设备上无效

iOS 约束导致 SIGABRT 错误

ios - 突出显示要按下的按钮,并取消突出显示未按下的按钮SWIFT

c++ - OpenGL 和 Xcode 颜色问题

objective-c - 以编程方式创建的 UIBarButtonItem 未启动选择器操作

objective-c - 取消所有本地通知;不适用于 iPhone 3GS

c++ - 由于忘记了源文件,在链接我自己的静态库时检测未解析的符号

ios - Swift - 使用来自 API 的数据填充 UITableView

swift - regex.matches 方法匹配和计数错误