https://github.com/robrix/box
Swift µframework of the ubiquitous Box<T> & MutableBox<T> reference types, for recursive value types & misc. other purposes.
https://github.com/robrix/box
Last synced: about 1 year ago
JSON representation
Swift µframework of the ubiquitous Box<T> & MutableBox<T> reference types, for recursive value types & misc. other purposes.
- Host: GitHub
- URL: https://github.com/robrix/box
- Owner: robrix
- License: mit
- Created: 2014-10-10T18:49:44.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2017-01-14T13:31:00.000Z (over 9 years ago)
- Last Synced: 2025-04-06T16:44:47.726Z (about 1 year ago)
- Language: Swift
- Homepage:
- Size: 52.7 KB
- Stars: 215
- Watchers: 5
- Forks: 31
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Box
This is a Swift microframework which implements `Box` & `MutableBox`, with implementations of `==`/`!=` where `T`: `Equatable`.
`Box` is typically used to work around limitations of value types:
- recursive `struct`s/`enum`s
- type-parameterized `enum`s where more than one `case` has a value
## Use
Wrapping & unwrapping a `Box`:
```swift
// Wrap:
let box = Box(1)
// Unwrap:
let value = box.value
```
Changing the value of a `MutableBox`:
```swift
// Mutation:
let mutableBox = MutableBox(1)
mutableBox.value = 2
```
Building a recursive value type:
```swift
struct BinaryTree {
let value: Int
let left: Box?
let right: Box?
}
```
Building a parameterized `enum`:
```swift
enum Result {
case Success(Box)
case Failure(NSError)
}
```
See the sources for more details.
## Integration
1. Add this repo as a submodule in e.g. `External/Box`:
git submodule add https://github.com/robrix/Box.git External/Box
2. Drag `Box.xcodeproj` into your `.xcworkspace`/`.xcodeproj`.
3. Add `Box.framework` to your target’s `Link Binary With Libraries` build phase.
4. You may also want to add a `Copy Files` phase which copies `Box.framework` (and any other framework dependencies you need) into your bundle’s `Frameworks` directory. If your target is a framework, you may instead want the client app to include `Box.framework`.