https://github.com/ivlevastef/swiftlazy
Classes (Lazy, Provider) is intended for late initialization.
https://github.com/ivlevastef/swiftlazy
Last synced: 4 months ago
JSON representation
Classes (Lazy, Provider) is intended for late initialization.
- Host: GitHub
- URL: https://github.com/ivlevastef/swiftlazy
- Owner: ivlevAstef
- License: mit
- Created: 2018-04-08T06:38:19.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-12-25T08:03:13.000Z (over 1 year ago)
- Last Synced: 2025-10-20T12:58:39.054Z (8 months ago)
- Language: Swift
- Size: 47.9 KB
- Stars: 14
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](http://cocoapods.org/pods/SwiftLazy)
[](https://github.com/Carthage/Carthage)
[](http://cocoapods.org/pods/SwiftLazy)
[](https://developer.apple.com/swift)
[](http://cocoapods.org/pods/SwiftLazy)
# SwiftLazy
Swift allows for lazy variables out-of-the-box, however they're fairly restricted.
## Features
* Lazy and Provider operations
* Short syntax
* Thread safe
## Usage
### Lazy
```Swift
let lazyInt = Lazy(1)
print(lazyInt.wasMade) // false
print(lazyInt.value) // 1
print(lazyInt.wasMade) // true
```
Support arithmetic operations:
```Swift
let lazyInt = Lazy{ 1 + 2 * 5 } * 4 + 10
print(lazyInt.wasMade) // false
print(lazyInt.value) // 54
print(lazyInt.wasMade) // true
```
Short syntax:
```Swift
let lazyInt = Lazy(1)
print(*lazyInt) // 1
```
### Provider
```Swift
let providerInt = Provider(1)
print(lazyInt.value) // 1
```
Support arithmetic operations:
```Swift
let providerInt = Provider{ 1 + 2 * 5 } * 4 + 10
print(providerInt.value) // 54
```
Short syntax:
```Swift
let providerInt = Provider(1)
print(*providerInt) // 1
```
### Difference Lazy vs Provider
```Swift
var counter: Int = 0
let lazyInt = Lazy {
counter += 1
return counter
}
print(lazyInt.value) // 1
print(lazyInt.value) // 1
print(lazyInt.value) // 1
lazyInt.clear() // no provider
print(lazyInt.value) // 2
print(lazyInt.value) // 2
print(lazyInt.value) // 2
```
```Swift
var counter: Int = 0
let providerInt = Provider {
counter += 1
return counter
}
print(providerInt.value) // 1
print(providerInt.value) // 2
print(providerInt.value) // 3
```
### Version 1.1.0
Add Provider with arguments:
```Swift
let provider = Provider2 { "\($0) + \($1) = \($0 + $1)" }
print(provider.value(10, 20.0)) // "10 + 20.0 = 30.0"
```
Support 1, 2, 3, 4, 5 arguments count.
## Install
###### Via CocoaPods.
To install SwiftLazy with CocoaPods, add the following lines to your Podfile: `pod 'SwiftLazy'`
###### Via Carthage.
github "ivlevAstef/SwiftLazy"
### The library is integrated with DITranquillity
## Requirements
iOS 11.0+,macOS 10.13+,tvOS 11.0+, watchOS 4.0+, Linux; ARC
## Feedback
### I've found a bug, or have a feature request
Please raise a [GitHub issue](https://github.com/ivlevAstef/SwiftLazy/issues).
### Question?
You can feel free to ask the question at e-mail: ivlev.stef@gmail.com.