Swift Extension 扩展

Swift Extension 扩展,第1张

概述Swift 里的Extension 和 OC 里的Catagory 差不多,很多细节会有差异,注释已写在代码里,请看注释 struct Point { var x = 0.0 var y = 0.0}struct Size { var width = 0.0 var height = 0.0}class Rectangle { var origi

Swift 里的Extension 和 OC 里的Catagory 差不多,很多细节会有差异,注释已写在代码里,请看注释

struct Point {    var x = 0.0    var y = 0.0}struct Size {    var wIDth = 0.0    var height = 0.0}class Rectangle {    var origin = Point()    var size = Size()        init(origin: Point,size: Size) {        self.origin = origin        self.size = size    }}// 扩展方法extension Rectangle {    func translate(x: Double,y: Double) -> VoID {        self.origin.x += x        self.origin.y += y    }}// Swift 只能扩展计算型的属性,不能扩展存储型的属性extension Rectangle {    var center: Point {        get {            let x = self.origin.x + self.size.wIDth/2            let y = self.origin.y + self.size.height/2            return Point(x: x,y: y)        }                set{            self.origin.x = newValue.x - self.size.wIDth/2            self.origin.y = newValue.y - self.size.height/2        }    }        // 只能扩展便利型的构造函数    convenIEnce init(center: Point,size: Size) {        self.init(origin: Point(x: center.x - size.wIDth/2,y: center.y - size.height/2),size: size)    }        //var borderWIDth:Double = 1.0 // 声明一个边框的属性直接报错 Extension may not contain stored propertIEs}// 嵌套枚举类型extension Rectangle {    enum Vertex {        case lefttop        case righttop        case leftBottom        case rightBottom    }        func getPointWithVertex(vertex: Vertex) -> Point {        switch vertex {        case .lefttop:            return self.origin                    case .righttop:            let x = self.origin.x + self.size.wIDth;            let y = self.origin.y            return Point(x: x,y: y);                case .leftBottom:            let x = self.origin.x            let y = self.origin.y + self.size.height            return Point(x: x,y: y);                    case .rightBottom:            let x = self.origin.x + self.size.wIDth;            let y = self.origin.y + self.size.height            return Point(x: x,y: y);                    }    }}var rect = Rectangle(origin: Point(x: 0,y: 0),size: Size(wIDth: 10,height: 10))print(rect.getPointWithVertex(vertex: Rectangle.Vertex.rightBottom)) // 两种方式都可以  ectangle.Vertex.rightBottom   .Vertex.rightBottomprint(rect.getPointWithVertex(vertex: .righttop))

上面代码运行结果:

Point(x: 10.0,y: 10.0)

Point(x: 10.0,y: 0.0)


可以给系统的原有类型做扩展

// double 的扩展extension Double{    var km: Double {        return self * 1000.0    }        var m:Double {        return self    }        var cm: Double {        return self/100.0    }        var ft: Double {        return self/3.2804    }}let runningdistance: Double = 5.00.kmprint(runningdistance)

运行结果:

5000.0

总结

以上是内存溢出为你收集整理的Swift Extension 扩展全部内容,希望文章能够帮你解决Swift Extension 扩展所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066566.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存