80

I have a variable that initialized as:

lazy var aClient:Clinet = {
    var _aClient = Clinet(ClinetSession.shared())
    _aClient.delegate = self
    return _aClient
}()

The problem is, at some point, I need to reset this aClient variable so it can initialize again when the ClinetSession.shared() changed. But if I set the class to optional Clinet?, LLVM will give me an error when I try to set it to nil. If I just reset it somewhere in the code using aClient = Clinet(ClinetSession.shared()), it will end up with EXEC_BAD_ACCESS.

Is there a way that can use lazy and being allowed to reset itself?

2
  • Just because I stumbled reading the code: is it Clinet and ClinetSession on purpose or is it a typo?
    – luk2302
    Commented May 27, 2016 at 17:10
  • 1
    @luk2302 typo, but guess it wasn't stop people from solving my question after all. :)
    – Cai
    Commented Sep 12, 2017 at 9:20

9 Answers 9

108

lazy is explicitly for one-time only initialization. The model you want to adopt is probably just an initialize-on-demand model:

var aClient:Client {
    if(_aClient == nil) {
        _aClient = Client(ClientSession.shared())
    }
    return _aClient!
}

var _aClient:Client?

Now whenever _aClient is nil, it will be initialized and returned. It can be reinitialized by setting _aClient = nil

5
  • This is clever! BTW I think we might needs to use self._aClient in the get{}. Sometimes Swift compiler can find a var in some cases if the self. is missing.
    – Cai
    Commented Aug 1, 2014 at 12:30
  • 6
    I had hoped for something better in Swift 2. What you really want is a var with a setter that only allows setting to nil, and getter code that is only called when the property is nil. Like var aClient:Client { setnil; getnil { aClient = Client (ClientSession.shared ()) } } getnil would be called if you get the variable after setting it to nil.
    – gnasher729
    Commented Jul 1, 2015 at 13:19
  • 1
    @gnasher729 Hooray for Swift 3! See my answer for exactly that :)
    – Ky -
    Commented Nov 30, 2016 at 18:25
  • Nice Answer...This method worked nicely for setting the GMSCircle which I created as Lazy var initially..Thank you!
    – BharathRao
    Commented Aug 13, 2019 at 12:20
  • If you use var _aClient:Client! then you can more easily configure _aClient in the getter.
    – malhal
    Commented Oct 22, 2020 at 12:39
47

Because the behavior of lazy changed in Swift 4, I wrote a few structs that give very specific behavior, which should never change between language versions. I put these on GitHub, under the BH-1-PD license: https://github.com/RougeWare/Swift-Lazy-Patterns

ResettableLazy

Here is the one relevant to this question, which gives you a way to lazily-initialize a value, cache that value, and destroy it so it can be lazily-reinitialized later.

Note that this requires Swift 5.1! For the Swift 4 version, see version 1.1.1 of that repo.

The simple usage of this is very straightforward:

@ResettableLazy
var myLazyString = "Hello, lazy!"

print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

_myLazyString.clear()
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

myLazyString = "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"
_myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value  "Hello, lazy!"

This will print:

Hello, lazy!
Hello, lazy!
Hello, lazy!
Hello, lazy!
Overwritten
Hello, lazy!

If you have complex initializer logic, you can pass that to the property wrapper:

func makeLazyString() -> String {
    print("Initializer side-effect")
    return "Hello, lazy!"
}

@ResettableLazy(initializer: makeLazyString)
var myLazyString: String

print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

_myLazyString.clear()
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

myLazyString = "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"
_myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value  "Hello, lazy!"

You can also use it directly (instaed of as a property wrapper):

var myLazyString = ResettableLazy<String>() {
    print("Initializer side-effect")
    return "Hello, lazy!"
}

print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"

myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"

myLazyString.wrappedValue = "Overwritten"
print(myLazyString.wrappedValue) // Just returns the value "Overwritten"
_myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value  "Hello, lazy!"

These will both print:

Initializer side-effect
Hello, lazy!
Hello, lazy!
Initializer side-effect
Hello, lazy!
Hello, lazy!
Overwritten
Initializer side-effect
Hello, lazy!

This answer has been updated; its original solution no longer works in Swift 4 and newer.

Instead, I recommend you use one of the solutions listed above, or @PBosman's solution

Previously, this answer hinged on behavior which was a bug. Both that old version of this answer, its behavior, and why it's a bug are described in the text and comments of Swift bug SR-5172 (which has been resolved as of 2017-07-14 with PR #10,911), and it's clear that this behavior was never intentional.

That solution is in that Swift bug's text, and also in the history of this answer, but because it's a bug exploit that doesn't work in Swift 3.2+ I recommend you do not do that.

24
  • 2
    IMHO, this is a cleaner way to achieve this than the accepted answer. Commented Dec 7, 2016 at 19:09
  • 1
    +1 for the usage of !, however, if one were to utilize this pattern, every caller of this variable would still have to unwrap the Optional. E.g., print(aClient) // prints "Optional(Clinet)\n" Commented Dec 14, 2016 at 19:56
  • 1
    I just tried it in a playground this morning, uploaded here: gist.github.com/pxpgraphics/4cfb7e02b6be7a583bf5f8a3ccbcd29a So maybe this is related to the string interpolation :] Commented Dec 14, 2016 at 21:21
  • 1
    I think I accidentally used this bug exploit... Thought it was very odd that long standing code only recently began causing crashes (recently updated to Swift 4). Are there strong advantages to the solution you linked to in your edit vs the accepted solution? Accepted solution seems to fit my needs, the one you linked to seems overly complex and I don't see the advantage of it.
    – Jake T.
    Commented May 2, 2018 at 16:14
  • 1
    @Ben Leggiero I appreciate the endorsement :) although, as I mentioned in my answer, I'm not totally happy with my version. I'd be interested to see what you got if you'd be willing to share it? Commented May 3, 2018 at 6:35
10

EDIT: As per Ben Leggiero's answer, lazy vars can be nilable in Swift 3. EDIT 2: Seems like nilable lazy vars are no more.

Very late to the party, and not even sure if this will be relevant in Swift 3, but here goes. David's answer is good, but if you want to create many lazy nil-able vars, you will have to write a pretty hefty block of code. I'm trying to create an ADT that encapsulates this behaviour. Here's what I've got so far:

struct ClearableLazy<T> {
    private var t: T!
    private var constructor: () -> T
    init(_ constructor: @escaping () -> T) {
        self.constructor = constructor
    }
    mutating func get() -> T {
        if t == nil {
            t = constructor()
        }
        return t
    }
    mutating func clear() { t = nil }
}

You would then declare and use properties like this:

var aClient = ClearableLazy(Client.init)
aClient.get().delegate = self
aClient.clear()

There are things I don't like about this yet, but don't know how to improve:

  • You have to pass a constructor to the initializer, which looks ugly. It has the advantage, though, that you can specify exactly how new objects are to be created.
  • Calling get() on a property every time you want to use it is terrible. It would be slightly better if this was a computed property, not a function, but computed properties cannot be mutating.
  • To eliminate the need to call get(), you have to extend every type you want to use this for with initializers for ClearableLazy.

If someone feels like picking it up from here, that would be awesome.

1
  • 1
    This is essentially what I do in Swift 4, now
    – Ky -
    Commented Jul 10, 2017 at 13:34
5

This allows setting the property to nil to force reinitialization:

private var _recordedFileURL: NSURL!

/// Location of the recorded file
private var recordedFileURL: NSURL! {
    if _recordedFileURL == nil {
        let file = "recording\(arc4random()).caf"
        let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(file)
        NSLog("FDSoundActivatedRecorder opened recording file: %@", url)
        _recordedFileURL = url
    }
    return _recordedFileURL
}
3

Swift 5.1:

class Game {
    private var _scores: [Double]? = nil

    var scores: [Double] {
        if _scores == nil {
            print("Computing scores...")
            _scores = [Double](repeating: 0, count: 3)
        }
        return _scores!
    }

    func resetScores() {
        _scores = nil
    }
}

Here is how to use:

var game = Game()
print(game.scores)
print(game.scores)
game.resetScores()
print(game.scores)
print(game.scores)

This produces the following output:

Computing scores...
[0.0, 0.0, 0.0]
[0.0, 0.0, 0.0]
Computing scores...
[0.0, 0.0, 0.0]
[0.0, 0.0, 0.0]

Swift 5.1 and Property Wrapper

@propertyWrapper
class Cached<Value: Codable> : Codable {
    var cachedValue: Value?
    var setter: (() -> Value)?

    // Remove if you don't need your Value to be Codable    
    enum CodingKeys: String, CodingKey {
        case cachedValue
    }

    init(setter: @escaping () -> Value) {
        self.setter = setter
    }

    var wrappedValue: Value {
        get {
            if cachedValue == nil {
                cachedValue = setter!()
            }
            return cachedValue!
        }
        set { cachedValue = nil }
    }

}

class Game {
    @Cached(setter: {
        print("Computing scores...")
        return [Double](repeating: 0, count: 3)
    })
    var scores: [Double]
}

We reset the cache by setting it to any value:

var game = Game()
print(game.scores)
print(game.scores)
game.scores = []
print(game.scores)
print(game.scores)
1
  • Look at the property wrapper alternative. The syntax is now much cleaner.
    – caram
    Commented Jul 21, 2019 at 17:15
2

There are some good answers here.
Resetting a lazy var is indeed, desirable in a lot of cases.

I think, you can also define a closure to create client and reset lazy var with this closure. Something like this:

class ClientSession {
    class func shared() -> ClientSession {
        return ClientSession()
    }
}

class Client {
    let session:ClientSession
    init(_ session:ClientSession) {
        self.session = session
    }
}

class Test {
    private let createClient = {()->(Client) in
        var _aClient = Client(ClientSession.shared())
        print("creating client")
        return _aClient
    }

    lazy var aClient:Client = createClient()
    func resetClient() {
        self.aClient = createClient()
    }
}

let test = Test()
test.aClient // creating client
test.aClient

// reset client
test.resetClient() // creating client
test.aClient
2

If the objective is to re-initialize a lazy property but not necessarily set it to nil, Building from Phlippie Bosman and Ben Leggiero, here is something that avoids conditional checks every time the value is read:

public struct RLazy<T> {
    public var value: T
    private var block: () -> T
    public init(_ block: @escaping () -> T) {
        self.block = block
        self.value = block()
    }
    public mutating func reset() {
        value = block()
    }
}

To test:

var prefix = "a"
var test = RLazy { () -> String in
    return "\(prefix)b"
}

test.value         // "ab"
test.value = "c"   // Changing value
test.value         // "c"
prefix = "d"
test.reset()       // Resetting value by executing block again
test.value         // "db"
0

I made @David Berry's answer into a property wrapper. Works great with UI-components that you want to reload if you need to apply size changes but otherwise want to hold in their configured state.

@propertyWrapper class Reloadable<T: AnyObject> {
  
  private let initializer:  (() -> T)
  private var _wrappedValue: T?
  var wrappedValue: T {
    if _wrappedValue == nil {
      _wrappedValue = initializer()
    }
    return _wrappedValue!
  }
  
  init(initializer: @escaping (() -> T)) {
    self.initializer = initializer
  }
  
  func nuke() {
    _wrappedValue = nil
  }
}

Here's an example with a CAShapeLayer. Set you variable like so:


@Reloadable<CAShapeLayer>(initializer: {
   Factory.ShapeLayer.make(fromType: .circle(radius: Definitions.radius, borderWidth: Definitions.borderWidth)) // this factory call is just what I use personally to build my components
}) private var circleLayer

and when you want to reload your view just call:

_circleLayer.nuke()

Then you can use the var circleLayer as you normally would in your re-layout routine, upon which it will get re-initialized.

PS: I made a gist for the file I use in my own project: https://gist.github.com/erikmartens/b34a130d11b62400ab13a59a6c3dbd91

0

This seems like a pretty bad code smell. Something strange is going on with:

var _aClient = Clinet(ClinetSession.shared())

What is ClinetSession.shared()?

This looks like a static function that returns a new instance of ClinetSession on every call.

So no wonder you are not seeing changes to this object. It seems like a broken singleton to me.

class ClinetSession {
    static func shared() -> Self {
        ClinetSession()
    }
}

Try doing this instead:

class ClinetSession {
    static let shared = ClinetSession()
    
    var value: Int = 0
}

Now if you change ClinetSession value you will see it.

There's no need for a lazy property that can be reset here as far as I can tell.

Here's a fully working example. BTW if you don't own ClinetSession then write a wrapper to control this.

class Clinet {
    let clinetSession: ClinetSession
    init(_ clinetSession: ClinetSession) {
        self.clinetSession = clinetSession
    }
    
    var delegate: P?
}

class ClinetSession {
    static let shared = ClinetSession()
    var value = 0
}

protocol P { }

struct Test: P {
    lazy var aClient:Clinet = {
        var _aClient = Clinet(ClinetSession.shared)
        _aClient.delegate = self
        return _aClient
    }()
    
    mutating func updateSession() {
        aClient.clinetSession.value = 10
    }
    
}

var test = Test()
test.updateSession()
print(test.aClient.clinetSession.value)

// prints 10

Note: If you don't want to use a singleton then don't use shared() as the constructor as this is a convention. But then it's up to you to make sure you pass in the same reference as the one you want to mutate. That's your job to manage. The singleton just makes sure there is only 1 instance so this becomes simpler but has its trade offs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.