How would you implement the following pattern in Swift?
The Container class is initialized with a JSON array that contains dictionaries. These dictionar
You could use an optional as the instance variable. Then create a function that returns the optional if it exists, and a new object if it does not to simulate lazy loading.
class Lazy {
var lazyVariable:String?
func lazilyGetEntries() -> String {
if let possibleVariable = self.lazyVariable { // optional already exists
return possibleVariable
}
else { // optional does not exist, create it
self.lazyVariable = String()
return self.lazyVariable!
}
}
}