swift setter causing exc_bad_access

后端 未结 3 1764
青春惊慌失措
青春惊慌失措 2020-12-07 01:49

I have a simple class below

import Foundation

public class UsefulClass: NSObject{
    var test:NSNumber{
        get{return self.test}
        set{
                


        
3条回答
  •  既然无缘
    2020-12-07 02:21

    Swift variables are synthesized properties by default. In the most cases this is sufficient (it's recommended to prefer Swift types)

    var test: Int
    
    override init() {
        super.init()
        test = 5
    }
    

    If you need to do something after a variable is set, use

    var test: Int {
        didSet{
            println("\(oldValue) - \(newValue)")
        }
    }
    

    your code sets the variable permanently by calling the setter which calls the setter which …

提交回复
热议问题