目录
目录README.md

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

关于
68.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号