Reputation: 2817
Why does this work, where PageBreak is a NSManagedObject
extension Sequence where Iterator.Element : PageBreak {
var landscape: [PageBreak] {
return self.filter({ (pageBreak) -> Bool in
return !pageBreak.isPortraitOrientation
})
}
var portrait: [PageBreak] {
return self.filter({ (pageBreak) -> Bool in
return pageBreak.isPortraitOrientation
})
}
}
But not this:
extension Sequence where Iterator.Element : String {
var onlyDumbOnes: [String] {
return self.filter({ (string) -> Bool in
if string.hasPrefix("Dumb") {
return true
}
return false
})
}
}
The compiler fails.
Value of type 'Self.Iterator.Element' has no member 'hasPrefix'
So it doesn't seem to know that we're dealing with String objects.
Upvotes: 4
Views: 45
Reputation: 154603
String
is a struct
and not a class
or a protocol
, so the syntax Iterator.Element : String
doesn't make sense since Iterator.Element
can't be a subclass of String
or implement the protocol
String
. Instead, use Iterator.Element == String
:
extension Sequence where Iterator.Element == String {
var onlyDumbOnes: [String] {
return self.filter { (string) -> Bool in
if string.hasPrefix("Dumb") {
return true
}
return false
}
}
}
Upvotes: 4