Box
Micro library for Swift that makes possible to transform any value type into a reference type.
Installation
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
Usage
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>
}
MutableBox
When using MutableBox
you can mutate the inner value.
struct User {
var email = "guilherme@grds.dev"
}
let user = MutableBox(wrappedValue: User())
func updateEmail(of user: MutableBox<User>) {
user.email = "newemail@grds.dev")
}
updateEmail(of: user)
assert(user.email == "newemail@grds.dev")
Both Box
and MutableBox
implements dynamicMemberLookup
so you can access any field from the wrapped type without "dereferencing" the box.
Contributing
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.