Extend array types using where clause in Swift
If you want to extend only array with specific type. You should extend _ArrayType protocol.
extension _ArrayType where Generator.Element == Int {
func doSomething() {
...
}
}
If you extend Array
you can only make sure your element is conformed some protocol else. i.e:
extension Array where Element: Equatable {
func doSomething() {
...
}
}
Updated:
With Swift 3.1 https://github.com/apple/swift/blob/master/CHANGELOG.md
extension Array where Element == Int {
func doSomething() {
...
}
}
Extension of Set where element is an Array of a specific type
Since you cannot make Array<NSOperation>
conform to Hashable
you'll have to make a small wrapper-struct.
E.g.
struct NSOperationList {
var operations = [NSOperation]()
}
and then build all functionality you need on top of NSOperationList
.
So if you want to add support for Set
:
extension NSOperationList: Hashable {
var hashValue: Int {
return operations.reduce(0) { $0 ^ $1.hashValue }
}
}
func == (a: NSOperationList, b: NSOperationList) -> Bool {
return a.operations == b.operations
}
Swift: Extend Array with generics method with namespace
Finally, I found a workaround.
I change the code in Array+CC.swift
:
extension Array: CCCompatible {}
extension CCWrapper where Wrapped: Sequence {
public func find(_ predicate: (Wrapped.Iterator.Element) -> Bool) -> Wrapped.Iterator.Element? {
for e in wrapped where predicate(e) { return e }
return nil
}
}
How to extend a CollectionType with a Protocol and a where clause?
You can define extension methods which apply only to a restricted
type of the generic placeholders
extension CollectionType where Generator.Element == DataPoint {
var barChartData: BarChartData { return somethingUseful }
}
and then
[DataPoint(), DataPoint()].barChartData
compiles. But you cannot declare a "conditional conformance to
a protocol", such as
extension CollectionType: DataPoint where Generator.Element == DataPoint { ... }
Such a feature is discussed on the Swift Evolution mailing list,
starting at [swift-evolution] [Manifesto] Completing Generics:
*Conditional conformances
Conditional conformances express the notion that a generic type will
conform to a particular protocol only under certain circumstances. For
example, Array is Equatable only when its elements are Equatable:extension Array : Equatable where Element : Equatable { }
but it is not available in Swift 2 and – as far as I can see –
not on the current lists of proposals for Swift 3 at https://github.com/apple/swift-evolution.
How can I extend typed Arrays in Swift?
For extending typed arrays with classes, the below works for me (Swift 2.2). For example, sorting a typed array:
class HighScoreEntry {
let score:Int
}
extension Array where Element == HighScoreEntry {
func sort() -> [HighScoreEntry] {
return sort { $0.score < $1.score }
}
}
Trying to do this with a struct or typealias will give an error:
Type 'Element' constrained to a non-protocol type 'HighScoreEntry'
Update:
To extend typed arrays with non-classes use the following approach:
typealias HighScoreEntry = (Int)
extension SequenceType where Generator.Element == HighScoreEntry {
func sort() -> [HighScoreEntry] {
return sort { $0 < $1 }
}
}
In Swift 3 some types have been renamed:
extension Sequence where Iterator.Element == HighScoreEntry
{
// ...
}
Related Topics
Round Trip Swift Number Types To/From Data
Separating Multiple If Conditions With Commas in Swift
Knowing Where Retain Cycles Are and Removing Them
Extensions in My Own Custom Class
Convert a Date (Absolute Time) to Be Sent/Received Across the Network as Data in Swift
Wrong Specialized Generic Function Gets Called in Swift 3 from an Indirect Call
How to Return an Object That I Create in My Data Service Class Through Firebase
Why Do Self and Self Sometimes Refer to Different Types in Static Functions
Noop For Swift'S Exhaustive Switch Statements
Difference Between Flatmap and Compactmap in Swift
Swift Structs to Nsdata and Back
How to Convert Array of Bytes [Uint8] into Hexa String in Swift
Trapping Signals in a Swift Command Line Application
Extra Arguments At Positions #11, #12 in Call Swiftui
Swift - Validating Uitextfield
Rounding a Double Value to X Number of Decimal Places in Swift