{"id":16853140,"url":"https://github.com/devxoul/typedkey","last_synced_at":"2025-04-10T22:41:48.678Z","repository":{"id":56924673,"uuid":"50592044","full_name":"devxoul/TypedKey","owner":"devxoul","description":"Statically-typed key for Swift.","archived":false,"fork":false,"pushed_at":"2016-03-06T08:31:31.000Z","size":18,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T19:39:28.244Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devxoul.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-28T15:24:38.000Z","updated_at":"2016-03-06T08:25:37.000Z","dependencies_parsed_at":"2022-08-20T22:50:22.000Z","dependency_job_id":null,"html_url":"https://github.com/devxoul/TypedKey","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FTypedKey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FTypedKey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FTypedKey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FTypedKey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devxoul","download_url":"https://codeload.github.com/devxoul/TypedKey/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248312208,"owners_count":21082638,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T13:49:53.887Z","updated_at":"2025-04-10T22:41:48.657Z","avatar_url":"https://github.com/devxoul.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"TypedKey\n========\n\n![Swift](https://img.shields.io/badge/Swift-2.1-orange.svg)\n[![Build Status](https://travis-ci.org/devxoul/TypedKey.svg)](https://travis-ci.org/devxoul/TypedKey)\n[![CocoaPods](http://img.shields.io/cocoapods/v/TypedKey.svg)](https://cocoapods.org/pods/TypedKey)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\nStatically-typed key for Swift.\n\n\nDesign Goal\n-----------\n\nA design goal of **TypedKey** is to provide statically typed key-value interface to Cocoa APIs.\n\n\nAt a Glance\n-----------\n\n##### Key Definition\n\n```swift\nlet myKey = Key\u003cString, Int\u003e(\"myKey\")\nmyKey.key // \"myKey\"\nmyKey.keyType // `String`\nmyKey.valueType // `Int`\n```\n\n##### NSUserDefaults\n\n```swift\nlet userIDKey = StringKey\u003cInt\u003e(\"userID\")\nNSUserDefaults.standardUserDefaults().setObject(10, forKey: userIDKey)\nNSUserDefaults.standardUserDefaults().objectForKey(userIDKey) // statically typed `Int?`\n```\n\n##### UITableView\n\n```swift\nlet userCellIdentifier = StringKey\u003cUserCell\u003e(\"userCell\")\ntableView.registerCell(userCellIdentifier)\ntableView.dequeueCell(userCellIdentifier) // statically typed `UserCell?`\n```\n\n##### UICollectionView\n\n```swift\nlet photoCellIdentifier = StringKey\u003cPhotoCell\u003e(\"photoCell\")\ncollection.registerCell(photoCellIdentifier)\ncollection.dequeueCell(photoCellIdentifier, forIndexPath: ...) // statically typed `PhotoCell?`\n```\n\n##### Anything Else?\n\nPull requests are welcomed 💖\n\n\nAdvanced Usage\n--------------\n\n#### Creating Custom Typed Key\n\nTo create your custom typed key, you should implement `TypedKey` protocol. For example, this is how `StringKey` is implemented:\n\n```swift\npublic struct StringKey\u003cValue\u003e: TypedKey {\n    public typealias KeyType = String\n    public typealias ValueType = Value\n\n    public let key: KeyType\n    public init(_ key: KeyType) {\n        self.key = key\n    }\n}\n```\n\n#### Creating Custom Interface\n\nYou 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`.\n\n```swift\nfunc set\u003cT: TypedKey\u003e(key: T, value: T.ValueType) {\n    // do something great\n}\n```\n\nA protocol `TypedKey` has two type aliases: `KeyType` and `ValueType`. You can make generic constraints on it.\n\n```swift\nfunc set\u003cT: TypedKey where T.KeyType == String, T.ValueType: AnyObject\u003e(key: T, value: T.ValueType) {\n    // do something great with String key and AnyObject value\n}\n```\n\n\nInstallation\n------------\n\n- **For iOS 8+ projects** with [CocoaPods](https://cocoapods.org):\n\n    ```ruby\n    pod 'TypedKey', '~\u003e 0.1.0'\n    ```\n\n- **For iOS 8+ projects** with [Carthage](https://github.com/Carthage/Carthage):\n\n    ```\n    github \"devxoul/TypedKey\" ~\u003e 0.1.0\n    ```\n\n- **For iOS 7 projects** with [CocoaSeeds](https://github.com/devxoul/CocoaSeeds):\n\n    ```ruby\n    github 'devxoul/TypedKey', '0.1.0', :files =\u003e 'Sources/*.swift'\n    ```\n\n- **Using [Swift Package Manager](https://swift.org/package-manager)**:\n\n    ```swift\n    import PackageDescription\n\n    let package = Package(\n        name: \"MyAwesomeApp\",\n        dependencies: [\n            .Package(url: \"https://github.com/devxoul/TypedKey\", \"0.1.0\"),\n        ]\n    )\n    ```\n\n\nLicense\n-------\n\n**TypedKey** is under MIT license. See the [LICENSE](LICENSE) file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevxoul%2Ftypedkey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevxoul%2Ftypedkey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevxoul%2Ftypedkey/lists"}