Micro library for Swift that makes possible to transform any value type into a reference type.
Box is distributed using Swift Package Manager. To install it into a project, add it as a dependency within your Package.swift
manifest:
let package = Package(
...
dependencies: [
.package(url: "https://github.com/grsouza/Box.swift.git", from: "1.0.0")
],
...
)
Then import Box wherever you'd like to use it:
import Box
There is 2 types in the library, Box
and MutableBox
, they are used to hold readonly reference and mutable reference respectively. Both types are property wrappers, so it can be used in the following ways.
class MyClass {
@Box(wrappedValue: "This is a Box String")
var value: String
let otherValue: Box<String>
}
When using MutableBox
you can mutate the inner value.
struct User {
var email = "[email protected]"
}
let user = MutableBox(wrappedValue: User())
func updateEmail(of user: MutableBox<User>) {
user.email = "[email protected]")
}
updateEmail(of: user)
assert(user.email == "[email protected]")
Both Box
and MutableBox
implements dynamicMemberLookup
so you can access any field from the wrapped type without "dereferencing" the box.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
link |
Stars: 5 |
Last commit: 2 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics