Swiftpack.co - Package - grsouza/Box.swift

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

License

Apache License 2.0

Github

link
Stars: 0

Dependencies

Used By

Total: 0

Releases

1.2.0 - 2020-09-30 01:39:26

Add

  • Add support for Cocoapods (#13)

1.1.1 - 2020-09-30 01:11:17

Changes

  • Add support for Swift 5.x (#10)

Internal

  • Fix unit tests when building for watchOS (#11)
  • Add compatibility badges to README (#12)

1.1.0 🎉 - 2020-08-18 12:08:57

Add:

  • [PR #7] Encodable and Decodable conformance to Box and MutableBox types.

1.0.1 - 2020-08-06 00:48:24

Changes:

  • Define platform and swiftLanguageVersions on Package.swift

1.0.0 🎉 - 2020-07-22 02:34:51

Adds Box and MutableBox types.