2

如果我想进行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
        }
    }

}
4

0 回答 0