Why does my @lazy property crash, but if I make it non lazy it works?

I have a problem with lazy properties. I thought I got them but maybe I didn’t / maybe it is a bug

I have a lazy array in my class

  • How to determine device type from Swift? (OS X or iOS)
  • Using threads to update UI with Swift
  • Get class name of object as string in Swift
  • BackBarButton won't display correct title
  • Swift optional inout parameters and nil
  • How to load animated GIF from photo library
  • @lazy var enteredRegions = Array<String>()
    

    now In

     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
    

    I have

     self.enteredRegions.append(clRegion!.identifier);
    

    this crashes hard with a EXC_BAD_ACCESS.

    NOW If I remove the @lazy:

        var enteredRegions = Array<String>()
    

    all works

    so.. whats up with this? 😀 my understanding was that self.enteredRegions would create it anyway

    (I do this with a UIWindow and a CLLocationManager and that works as I thought)

    Solutions Collect From Internet About “Why does my @lazy property crash, but if I make it non lazy it works?”

    After some trial, I found something quite odd.

    First of all, if we wrap the var inside a class, it simply works:

    class RegionManager {
        @lazy var enteredRegions = Array<String>()
    }
    

    In AppDelegate, I declared a @lazy var regManager = RegionManager().

    Then, in application:didFinishLaunching:, I modify and use the value, it works without spitting a word:

    regManager.enteredRegions.append("New!")
    println("Regions: \(regManager.enteredRegions)") // Regions: [New!]
    

    After this, I tried to change to some native values, for example, String, Int and so on, they all fail.

    So, my guess is that this strange behaviour is actually a bug of the compiler itself, it may be rooted in some optimization for native types, maybe there’s a inner pool or what that apple doesn’t tell us.