Swift

创建模板类(封装一个类)

例1:
新建一个名字叫做 Product 的类

Product.swift File 的内容

class Product {
    var name: String
    var description: String
    var price: Double
    var stock: Int
    init(name: String, description: String, price: Double, stock: Int) {
        self.name = name
        self.description = description
        self.price = price
        self.stock = stock
    }
}

例2:

override func viewDidLoad() {

        super.viewDidLoad()

        let circle = CircleView(frame: CGRectMake(100, 150, 150, 150))

        self.view.addSubview(circle)

        self.addLabel(circle1.frame, text: "X", fontSize: 60)

    }

    func addLabel(frame: CGRect, text: String, fontSize: CGFloat) {

        let label = UILabel(frame: frame)

        label.text = text

        label.textAlignment = .Center

        label.font = UIFont.systemFontOfSize(fontSize, weight: 10)

        self.view.addSubview(label)

    }

 
原文地址:https://www.cnblogs.com/gongyuhonglou/p/10311586.html