【发布时间】:2016-03-16 17:48:46
【问题描述】:
我正在整理我在应用中使用的一些常用功能,并提出了以下扩展:
public extension CollectionType {
public func toArray() -> [Self.Generator.Element] {
return self.map { $0 }
}
}
public extension Array {
public func any(fn: (Element) -> Bool) -> Bool {
return self.filter(fn).count > 0
}
public func all(fn: (Element) -> Bool) -> Bool {
return self.filter(fn).count == self.count
}
public func take(count:Int) -> [Element] {
var to = [Element]()
var i = 0
while i < self.count && i < count {
to.append(self[i++])
}
return to
}
public func skip(count:Int) -> [Element] {
var to = [Element]()
var i = count
while i < self.count {
to.append(self[i++])
}
return to
}
}
它们可以应用于像SequenceType 这样的较低级别的类型吗?另外,我必须将@noescape 放在这些函数的任何位置吗?
【问题讨论】:
标签: arrays swift swift2 swift-extensions