I am trying to achieve this objective-c code
@property (strong) UIView *customView; -(UIView*)customView { if (!customView){ self.customView = [[UIView alloc]init]; self.customView.backgroundColor = [UIColor blueColor]; } return customView; }
Why am I using this? customView is called from many places, so we need to check this condition all over the place. To avoid this duplication, I wrote this.
So, I'm trying to create stored properties, and also use the getter method to check if it is already created or not.
var mainView : UIView? { get{ if let customView = self.mainView{ return self.mainView } var customView : UIView = UIView() self.mainView = customView self.mainView.backgroundColor = UIColor(blueColor) return customView } set{ self.mainView = newValue } }
It is right? or any other approach for this?
Note. There are no warnings or errors with the above code. But confusion with stored and computed properties. Please let me know.
source share