Latest 1.0.1
Homepage https://github.com/BellAppLab/Laziable
License MIT
Platforms ios 9.0, watchos 3.0, osx 10.10, tvos 9.0
Frameworks Foundation
Authors

Laziable Version License

Platforms
Swift support
CocoaPods Compatible
Carthage compatible
Twitter

Laziable

So you declared a lazy var in Swift thinking it would behave like lazily instantiated variables in good ol’ Objective-C. You thought you would set them to nil and they would reconstruct themselves later on when needed.

You poor thing.

They don’t.

So why not bring that awesomeness back to Swift in a very lightweight way?

Specs

  • iOS 9+
  • watchOS 3+
  • tvOS 9+
  • watchOS 3+
  • macOS 10.10+
  • Swift 3.3+

Usage

Declare your Lazy variable in one of the three ways provided:

Suggestion: for the best results, use let when declaring your Lazy variables.

class TestClass
{
    let lazyString = §{
        return "testString"
    }

    let lazyDouble: Lazy<Double> = Lazy {
        return 0.0
    }

    let lazyArray = Lazy {
        return ["one", "two", "three"]
    }
}

Access your variable:

let testObject = TestClass()
print(testObject.lazyString§) //prints out "testString"

Set your variable to nil, so it gets reconstructed again later:

let testObject = TestClass()
testObject.lazyDouble §= nil

Operators

  • prefix §
    • Shorthand contructor for a Lazy variable:
let lazyThing = §{
    return <#code#>
}
  • postfix operator §
    • Shorthand accessor for Lazy:
let lazyString = §{
    return "Much cool"
}

print(lazyThing§) //prints out "Much cool"
  • infix operator §=
    • Shorthand assignment for Lazy:
let lazyString = §{
    return "Much cool"
}

lazyString §= nil //the string "Much cool" has been destroyed
print(lazyThing§) //reconstructs the string and prints out "Much cool"

Notes

For the best results, use let when declaring your Lazy variables.

Also, make sure to use [weak self] or [unowned self] if capturing self in a Lazy variable’s constructor.

Installation

Cocoapods

pod 'Laziable', '~> 1.0'

Then import Laziable where needed.

Carthage

github "BellAppLab/Laziable" ~> 1.0

Then import Laziable where needed.

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/BellAppLab/Laziable", from: "1.0.0")
]

Then import Laziable where needed.

Git Submodules

cd toYourProjectsFolder
git submodule add -b submodule --name Laziable https://github.com/BellAppLab/Laziable.git

Then drag the Laziable folder into your Xcode project.

Author

Bell App Lab, [email protected]

Credits

Logo image by Georgiana Ionescu from The Noun Project

License

Lazy is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Laziable",
    "version": "1.0.1",
    "summary": "When 'lazy var' doesn't cut it, have a truly Lazy variable in Swift.",
    "screenshots": "https://github.com/BellAppLab/Laziable/raw/master/Images/laziable.png",
    "description": "So you declared a `lazy var` in Swift thinking it would behave like lazily instantiated variables in good ol' Objective-C. You thought you would set them to `nil` and they would reconstruct themselves later on when needed.nnYou poor thing.nn[They don't](https://stackoverflow.com/a/40847994).nnSo why not bring that awesomeness back to Swift in a very lightweight way?",
    "homepage": "https://github.com/BellAppLab/Laziable",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Bell App Lab": "[email protected]"
    },
    "social_media_url": "https://twitter.com/BellAppLab",
    "platforms": {
        "ios": "9.0",
        "watchos": "3.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "module_name": "Laziable",
    "source": {
        "git": "https://github.com/BellAppLab/Laziable.git",
        "tag": "1.0.1"
    },
    "source_files": "Sources/Laziable",
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This