Extensions work with computed properties.
Extensions in Swift do not support stored properties.
Consider the following code
class Distance { var miles = 0.0 var km: Double { get {/*from www. j a v a 2 s.co m*/ return 1.60934 * miles } set (km) { miles = km / 1.60934 } } }
You could extend the Distance class by adding computed properties to it:
extension Distance { var feet: Double { return miles * 5280 } var yard: Double { return miles * 1760 } }
In the preceding code snippet, you added two new computed properties to the Distance class:
You can use the newly added computed properties as shown here:
class Distance { var miles = 0.0 var km: Double { get {//from ww w . j ava 2 s .co m return 1.60934 * miles } set (km) { miles = km / 1.60934 } } } extension Distance { var feet: Double { return miles * 5280 } var yard: Double { return miles * 1760 } } var d = Distance () d.miles = 10 print(d.feet) //52800.0 print(d.yard) //17600.0