https://github.com/devxoul/typedkey
Statically-typed key for Swift.
https://github.com/devxoul/typedkey
Last synced: 3 months ago
JSON representation
Statically-typed key for Swift.
- Host: GitHub
- URL: https://github.com/devxoul/typedkey
- Owner: devxoul
- License: mit
- Created: 2016-01-28T15:24:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-06T08:31:31.000Z (over 9 years ago)
- Last Synced: 2025-03-24T19:39:28.244Z (3 months ago)
- Language: Swift
- Size: 17.6 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
TypedKey
========
[](https://travis-ci.org/devxoul/TypedKey)
[](https://cocoapods.org/pods/TypedKey)
[](https://github.com/Carthage/Carthage)Statically-typed key for Swift.
Design Goal
-----------A design goal of **TypedKey** is to provide statically typed key-value interface to Cocoa APIs.
At a Glance
-----------##### Key Definition
```swift
let myKey = Key("myKey")
myKey.key // "myKey"
myKey.keyType // `String`
myKey.valueType // `Int`
```##### NSUserDefaults
```swift
let userIDKey = StringKey("userID")
NSUserDefaults.standardUserDefaults().setObject(10, forKey: userIDKey)
NSUserDefaults.standardUserDefaults().objectForKey(userIDKey) // statically typed `Int?`
```##### UITableView
```swift
let userCellIdentifier = StringKey("userCell")
tableView.registerCell(userCellIdentifier)
tableView.dequeueCell(userCellIdentifier) // statically typed `UserCell?`
```##### UICollectionView
```swift
let photoCellIdentifier = StringKey("photoCell")
collection.registerCell(photoCellIdentifier)
collection.dequeueCell(photoCellIdentifier, forIndexPath: ...) // statically typed `PhotoCell?`
```##### Anything Else?
Pull requests are welcomed 💖
Advanced Usage
--------------#### Creating Custom Typed Key
To create your custom typed key, you should implement `TypedKey` protocol. For example, this is how `StringKey` is implemented:
```swift
public struct StringKey: TypedKey {
public typealias KeyType = String
public typealias ValueType = Valuepublic let key: KeyType
public init(_ key: KeyType) {
self.key = key
}
}
```#### Creating Custom Interface
You can make static typing key-value interface with **TypedKey**. Functions should use generic with type constraint on `TypedKey`. A type of the value can be inferred from the generic by using `ValueType`.
```swift
func set(key: T, value: T.ValueType) {
// do something great
}
```A protocol `TypedKey` has two type aliases: `KeyType` and `ValueType`. You can make generic constraints on it.
```swift
func set(key: T, value: T.ValueType) {
// do something great with String key and AnyObject value
}
```Installation
------------- **For iOS 8+ projects** with [CocoaPods](https://cocoapods.org):
```ruby
pod 'TypedKey', '~> 0.1.0'
```- **For iOS 8+ projects** with [Carthage](https://github.com/Carthage/Carthage):
```
github "devxoul/TypedKey" ~> 0.1.0
```- **For iOS 7 projects** with [CocoaSeeds](https://github.com/devxoul/CocoaSeeds):
```ruby
github 'devxoul/TypedKey', '0.1.0', :files => 'Sources/*.swift'
```- **Using [Swift Package Manager](https://swift.org/package-manager)**:
```swift
import PackageDescriptionlet package = Package(
name: "MyAwesomeApp",
dependencies: [
.Package(url: "https://github.com/devxoul/TypedKey", "0.1.0"),
]
)
```License
-------**TypedKey** is under MIT license. See the [LICENSE](LICENSE) file for more info.