Swift: 'var' declaration without getter/setter method not allowed here

前端 未结 2 1972
-上瘾入骨i
-上瘾入骨i 2021-02-05 15:01

I\'ve tried to declare IBOutlet property on extension of class. But it give error as

\'var\' declaration without getter/setter method not al

2条回答
  •  不思量自难忘°
    2021-02-05 15:41

    From Extensions -> Computed Properties in The Swift Programming Language

    NOTE

    Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.


    Addition in response to twlkyao's comment: Here is my implementation of the absoluteValue property of a Double

    extension Double {
        var absoluteValue: Double {
            if self >= 0 {
                return self
            } else {
                return -self
            }
        }
    }
    
    // Simple test -> BOTH println() should get called.
    var a = -10.0
    if (a < 0) {
        println("Smaller than Zero")
    }
    if (a.absoluteValue > 5) {
        println("Absolute is > 5")
    }
    

提交回复
热议问题