Swiftpack.co - grdsdev/swift-box as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by grdsdev.
grdsdev/swift-box 1.2.0
Box type for Swift, transform any value type into a reference type.
⭐️ 5
🕓 3 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/grdsdev/swift-box.git", from: "1.2.0")

Box

Swift

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 = "[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.

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.

License

Apache License 2.0

GitHub

link
Stars: 5
Last commit: 2 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Release Notes

1.2.0
3 years ago

Add

  • Add support for Cocoapods (#13)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics