坦率地说,我对扩展的创建和使用是完全陌生的。
我想创建一个类别(swift 3.0中的扩展),可以在整个应用程序中使用它来执行Array的重复操作。
示例链接1
这是我在做研究时所看到和理解的,我想用各种方法创建一个扩展,这些方法应该是通用的,而不是基于创建单独扩展所需的数据类型。
在上面的例子中,如果我们要进行特定的数据类型扩展,我们将需要创建单个扩展。如果有任何方法可以创建通用类别(swift中的扩展),我想有一个指导。
扩展_ArrayTypeGenerator. Element==Int
扩展数组,其中元素:可均衡
扩展数组,其中Element==Int
扩展_ArrayTypeGenerator. Element==Float
扩展SequenceType其中Self. Generator.Element:FloatingPointType
扩展数组,其中元素:DoubleValue
扩展Iterator. Element==String
,等等…
示例链接2
注意:简而言之,我们可以考虑在单个扩展中执行基于Array的操作,而不是按照上述要求为每个数据类型创建单个扩展。
如注释中所述,实现此目的的一种方法是创建您自己的协议,您要涵盖的类型采用该协议(在注释中,有人将其称为Content
,在此示例中使用如下)(来自第一个来源):
protocol Content {
var hash: String { get }
}
extension Array where Element : Content {
func filterWithId(id : String) -> [Element] {
return self.filter { (item) -> Bool in
return item.id == id
}
}
}
不过,最初的问题似乎主要是询问数组的通用扩展,一条评论说这是不可能的,但在Swift中100%是可能的(实际上这是一个很大的Swift特性)(来自第二个来源)。
例如,如果您只想为Int
定义特定的扩展方法,您可以这样做:
extension Sequence where Iterator.Element == Int {
var sum: Int {
return reduce(0, +)
}
}
这个问题的最初要求似乎是扩展方法,这些方法可能与数据类型无关,因此应该保持通用。如果我理解正确,似乎这些数据类型通常与Equable
和/或Hasable
有一些一致性,这是这种泛型工作的最低要求。然而,有了这种元素一致性,这是可能的:
extension Sequence where Iterator.Element is Equatable {
func extensionMethodName<T: Equatable>(_ input: [T], singleElement: T) -> [T] {
// T is now a generic array of equatable items. You can implement whatever extension logic you need with these.
// I added different ways of passing in and returning this generic type, but the only thing that is likely going to be consistent is the `<T: Equatable>` which is Swift standard syntax for declaring generic type parameters for a method.
}
}
Swift语法变化很快,这里的内容可能很快就会过时,但本指南由Apple保持相当最新,并显示了上面使用的Generics
的最新语法^。
我的答案来自几个StackOverflow
问题/答案,用于上面的示例/语法^。来源:(SOSource)(SOSource 2)
总之,上述所有方法都可以组合使用,以获得一个完全自定义的扩展解决方案,该解决方案具有所有Array
类型的泛型函数/var,同时仍然具有特定于类型的扩展覆盖。
在where子句中,您指定“如果元素类型具有这些规则,请考虑此扩展”。
您不需要在所有扩展中实现所有方法。
例如:
>
您想扩展数组
extension Array {
func foo(bar: Element) { /*your code goes here */ }
}
你想扩展数组
extension Array where Element: Equatable {
func find(value: Element) -> Bool {
return index(of: value) != nil
}
}
您想在Element
是数字
的情况下扩展Sequence
,具有仅获取变量sum:
extension Sequence where Element: Numeric {
var sum: Element {
return reduce(0, +)
}
}
您想扩展Collection
extension Collection
where Iterator.Element: Collection,
Iterator.Element.Iterator.Element: Equatable {
func compare(to: Self) -> Bool {
let flattenSelf = self.reduce([], +)
let flattenTo = to.reduce([], +)
return flattenSelf.count == flattenTo.count &&
zip(flattenSelf, flattenTo).reduce(true) { $0 && $1.0 == $1.1 }
}
}
你不需要扩展数组或集合来拥有像sort
、查找
等方法…如果你的元素:可平衡
或元素:可比较
,这些方法中的大多数已经在编译器中扩展了。使用map
、filter
和duce
,你可以用不太多的代码实现更复杂的结构。