如果我想进行Array
扩展,但只Array
对 type进行扩展[UIView]
,这在 Swift 中是否可行?
目前我只是在做一个普通extension Array
的,前提是Array
只包含 type 的元素UIView
,但似乎必须有更好的方法。
这是我目前的做法:
extension Array {
mutating func sortByVerticalPositionTopToBottomIn(rootView: UIView) {
precondition(arrayConsistsOfOnlyType(UIView), "This method should only be used for arrays of UIViews.")
self.sort {
let firstObj = $0 as! UIView
let secondObj = $1 as! UIView
return firstObj.convertPoint(CGPointZero, toView: rootView).y < secondObj.convertPoint(CGPointZero, toView: rootView).y
}
}
}