2 ms·
Souns like get/set in swift for me. Does it have computed properties too?
by DelightOne 9y ago
Souns like get/set in swift for me. Does it have computed properties too?
- taspeotis 9y agopublic double Area { get { return Width * Height; } set { Width = Math.Sqrt(value); Height = Width; } } You can also use expression bodied members (that's the => syntax) with get/set. There's a shorthand if you only need to define the getter (i.e. read-only): public double Area => Width * Height;
- DelightOne 9y agoSwift has the same syntax too except the last line. Have they introducted willSet and didSet too? Neat stuff, i may try the language out in a future project on windows.
- taspeotis 9y agoThere's no direct equivalent to willSet or didSet. The closest equivalent (aside from adding code before/after your own get/set code) might be INotifyPropertyChanged [1]/INotifyPropertyChanging [2]? But they are more for external observers and add a lot of boilerplate to your code. It's possible to manage the boilerplate, however [3]. [1] https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx#Examples https://msdn.microsoft.com/en-us/library/system.componentmod... [2] https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanging(v=vs.110).aspx#Remarks https://msdn.microsoft.com/en-us/library/system.componentmod... [3] https://github.com/Fody/PropertyChanged https://github.com/Fody/PropertyChanged