nil check inside getter method in swift?

Viewed 3176

I have an array in my view controller. It should be allocated only if it is nil otherwise it should return the existing value.

Objective C equivalent:

- (NSArray*)states{
     if(!_states)
     {
         _states = //read files from json and assigned to array
     }
     return _states;
}

I have to achieve this in swift. I tried with stored property but not able to achieve this

what is the best way to achieve this

3 Answers
Related