As for now, you cannot write an extension of Array
"whose Element
is always a String
" in Swift.
But you can write some extension which has nearly the same functionality.
Write your own protocol, which String
can conform to:
protocol MyStringType {
var characters: String.CharacterView { get }
//You may need some other properties or methods to write your extension...
}
// Make `String` the only type conforming to `MyStringType`.
extension String: MyStringType {}
And write an extension where Element
conforms to the protocol.
extension Array where Element: MyStringType {
func spliteByPrefix() -> [Element]{ //You really want to return, `Array<Element>`?
for item in self {
for ch in item.characters {
//Do something with `ch`.
_ = ch
}
}
return []
}
}