I am trying to write an extension method for [String] .
It seems you can't directly extend [String] (the "Element Type" is bound to the non-protocol "String" type), although I came across this trick:
protocol StringType { } extension String: StringType { }
But I still can't make Swift happy with this:
extension Array where Element: StringType { // ["a","b","c","d","e"] -> "a, b, c, d, or e". func joinWithCommas() -> String { switch count { case 0, 1, 2: return joinWithSeparator(" or ") default: return dropLast(1).joinWithSeparator(", ") + ", or " + last! } } }
The joinWithSeparator calls are "Ambiguous." I tried everything that I could think of, for example, using (self as! [String]) (and a bunch of similar options), but nothing works.
How can I make the Swift compiler this?
swift
J. Cocoe Jul 23 '16 at 19:19 2016-07-23 19:19
source share