Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nickffox/identifierkit

Strongly typed identifiers for Swift projects
https://github.com/nickffox/identifierkit

ios swift swift-framework swift4 typesafety

Last synced: 29 days ago
JSON representation

Strongly typed identifiers for Swift projects

Awesome Lists containing this project

README

        

![IdentifierKit](https://raw.githubusercontent.com/NicholasFFox/IdentifierKit/master/identifierkit.png)

IdentifierKit allows you to easily replace `String` and `Int` identifier with strongly typed identifiers.

- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Thanks](#thanks)
- [License](#license)

## Features

- [x] 💪 Strongly typed identifiers
- [x] 🐟 Full `Codeable` compatibility

## Requirements

* iOS 8.0+
* Xcode 9.2+
* Swift 4+

## Installation

### CocoaPods

`pod 'IdentifierKit'`

### Carthage

`github "NicholasFFox/IdentifierKit"`

### Swift Package Manager

* Coming Soon

## Usage

Use IdentifierKit to replace weakly typed `String` and `Int` identifiers in your model layer with strongly typed identifier objects.

Step 1: Create an unique type to represent your identifier - empty enums work great for this!

```Swift
enum UserIdentifier: Identifier {}
```

Step 2: Use a typealias to give yourself syntactic sugar and avoid typing the generic syntax everywhere.
```Swift
typealias UserId = IntIdentifier
```

Step 3: Replace your `Int` (or `String`) identifer with your newly created type.
```Swift
struct User: Codable {
let id: UserId
let firstName: String
let lastName: String
}
```

IdentifierKit works seamlessly with the `Codable` protocol, so you won't have to make any changes to your existing JSON.

```Swift
let json = """
{
"id": 2,
"firstName": "Tim",
"lastName": "Cook"
}
""".data(using: .utf8)!

let user = try JSONDecoder().decode(User.self, from: json)
```

## Thanks

Thanks to [Daniel Steinberg](https://www.twitter.com/dimsumthinking) for inspiring this framework with your talk on phantom types.

## License

IdentifierKit is released under the MIT license. See [LICENSE](https://github.com/NicholasFFox/IdentifierKit/blob/master/LICENSE) for details.