Swift 成员变量的get/set

import UIKit

class Question: NSObject {
    
    var answer: String!
    var icon: String!
    var title: String!
    var options: [String]!
    
    // 自定义 get、set
    var image: UIImage {
        get {
            return UIImage(named: self.icon)!
        }
        
        set {
            self.image = newValue
        }
    }
    
    // 懒加载
    lazy private var questions: [AnyObject] = {
        // 例如:
        return ["quetion1", "quetion2", "quetion3"]
    }()

    // 指定初始化
    init(data: [String : AnyObject]) {
        super.init()

        self.setValuesForKeysWithDictionary(data)
    }
    
}

你可能感兴趣的:(Swift 成员变量的get/set)