对于使用 类 扩展类型数组,以下内容对我有用(Swift 2.2 )。例如,对类型化数组进行排序:
class HighScoreEntry { let score:Int}extension Array where Element == HighScoreEntry { func sort() -> [HighScoreEntry] { return sort { $0.score < $1.score } }}尝试使用 struct 或 typealias 执行此 操作 将产生错误:
Type 'Element' constrained to a non-protocol type 'HighScoreEntry'
更新 :
要使用 非类 扩展类型化数组,请使用以下方法:
typealias HighScoreEntry = (Int)extension SequenceType where Generator.Element == HighScoreEntry { func sort() -> [HighScoreEntry] { return sort { $0 < $1 } }}在 Swift 3中, 某些类型已重命名:
extension Sequence where Iterator.Element == HighScoreEntry { // ...}


