{"id":18259072,"url":"https://github.com/ddddxxx/genericid","last_synced_at":"2025-10-08T09:13:25.180Z","repository":{"id":64060148,"uuid":"89551357","full_name":"ddddxxx/GenericID","owner":"ddddxxx","description":"A Swift extension to use string-based API in a type-safe way.","archived":false,"fork":false,"pushed_at":"2021-07-08T12:29:23.000Z","size":168,"stargazers_count":13,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-09-10T19:52:16.697Z","etag":null,"topics":["userdefaults"],"latest_commit_sha":null,"homepage":"","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/ddddxxx.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":"2017-04-27T03:22:38.000Z","updated_at":"2022-11-10T11:42:55.000Z","dependencies_parsed_at":"2022-12-01T20:03:09.485Z","dependency_job_id":null,"html_url":"https://github.com/ddddxxx/GenericID","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/ddddxxx/GenericID","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddddxxx%2FGenericID","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddddxxx%2FGenericID/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddddxxx%2FGenericID/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddddxxx%2FGenericID/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ddddxxx","download_url":"https://codeload.github.com/ddddxxx/GenericID/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddddxxx%2FGenericID/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278918114,"owners_count":26068409,"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","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["userdefaults"],"created_at":"2024-11-05T10:36:09.127Z","updated_at":"2025-10-08T09:13:25.160Z","avatar_url":"https://github.com/ddddxxx.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e This project is currently in beta and APIs are subject to change.\n\n# GenericID\n\n![platforms](https://img.shields.io/badge/platforms-macOS%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS-lightgrey.svg)\n![supports](https://img.shields.io/badge/Swift_Package_Manager-compatible-brightgreen.svg)\n![swift](https://img.shields.io/badge/swift-4.0-orange.svg)\n[![codebeat badge](https://codebeat.co/badges/2bf7d7e0-2bfe-4280-bbb3-ed64566ddd10)](https://codebeat.co/projects/github-com-ddddxxx-genericid-master)\n\nA Swift extension to use string-based API in a **type-safe** way.\n\nAll these fantastic API are compatible with traditional string-based API.\n\n## Requirements\n\n- Swift 5.2 (Xcode 11.4)\n\n## Type-safe `UserDefaults`\n\n\u003e You can use `NSUbiquitousKeyValueStore` with almost the same API.\n\n### 1. Define your keys\n\n```swift\nextension UserDefaults.DefaultKeys {\n    static let intKey   = Key\u003cInt\u003e(\"intKey\")\n    static let colorKey = Key\u003cColor\u003e(\"colorKey\", transformer: .keyedArchive)\n    static let pointKey = Key\u003cCGPoint\u003e(\"pointKey\", transformer: .json)\n}\n```\n\n### 2. Have fun!\n\n```swift\nlet ud = UserDefaults.standard\n\n// Get \u0026 Set\nlet value = ud[.intKey]\nud[.stringKey] = \"foo\"\n\n// Modify\nud[.intKey] += 1\nud[.stringKey] += \"bar\"\n\n// Typed array\nud[.stringArrayKey].contains(\"foo\")\nud[.intArrayKey][0] += 1\n\n// Work with NSKeyedArchiver\nud[.colorKey] = UIColor.orange\nud[.colorKey]?.redComponent\n\n// Work with JSONEncoder\nud[.pointKey] = CGPoint(x: 1, y: 1)\nud[.pointKey]?.x += 1\n\n// Modern Key-Value Observing\nlet observation = defaults.observe(.someKey, options: [.old, .new]) { (defaults, change) in\n    print(change.newValue)\n}\n\n// KVO with deserializer\nlet observation = defaults.observe(.rectKey, options: [.old, .new]) { (defaults, change) in\n    // deserialized automatically\n    if let rect = change.newValue {\n        someView.frame = rect\n    }\n}\n\n// Register with serializer\nud.register(defaults: [\n    .intKey: 42,\n    .stringKey: \"foo\",\n    .colorKey: UIColor.blue, // serialized automatically\n    .pointKey: CGPoint(x: 1, y: 1),\n])\n```\n\n### Default value\n\nIf associated type of a key conforms `DefaultConstructible`, a default value will be constructed for `nil` result.\n\n```swift\npublic protocol DefaultConstructible {\n    init()\n}\n```\n\nHere's types that conforms `DefaultConstructible` and its default value:\n\n| Type          | Default value |\n|---------------|---------------|\n| Bool          | `false`       |\n| Int           | `0`           |\n| Float/Double  | `0.0`         |\n| String        | `\"\"`          |\n| Data          | [empty data]  |\n| Array         | `[]`          |\n| Dictionary    | `[:]`         |\n| Optional      | `nil`         |\n\nNote: `Optional` also conforms `DefaultConstructible`, therefore a key typed as `DefaultKey\u003cAny?\u003e` aka `DefaultKey\u003cOptional\u003cAny\u003e\u003e` will still returns `nil`, which is the result of default construction of `Optional`.\n\nYou can always associate an optional type if you want an optional value.\n\n\u003c!--### Observing--\u003e\n\n## Type-safe `UITableViewCell` / `UICollectionViewCell`\n\n### 1. Define your reuse identifiers\n\n```swift\nextension UITableView.CellReuseIdentifiers {\n    static let customCell : ID\u003cMyCustomCell\u003e = \"CustomCellReuseIdentifier\"\n}\n```\n\n### 2. Register your cells\n\n```swift\ntableView.register(id: .customCell)\n```\n\n### 3. Dequeue your cells\n\n```swift\nlet cell = tableView.dequeueReusableCell(withIdentifier: .customCell, for: indexPath)\n// Typed as MyCustomCell\n```\n\n### XIB-based cells\n\n```swift\n// That's it!\nextension MyCustomCell: UINibFromTypeGettable\n\n// Or, incase your nib name is not the same as class name\nextension MyCustomCell: UINibGettable {\n    static var nibName = \"MyNibName\"\n}\n\n// Then register\ntableView.registerNib(id: .customCell)\n```\n\n## Type-safe Storyboard\n\n### 1. Define your storyboards identifiers\n\n```swift\nextension UIStoryboard.Identifiers {\n    static let customVC: ID\u003cMyCustomViewController\u003e = \"CustomVCStoryboardIdentifier\"\n}\n```\n\n### 2. Use It!\n\n```swift\n// Also extend to get main storyboard\nlet sb = UIStoryboard.main()\n\nlet vc = sb.instantiateViewController(withIdentifier: .customVC)\n// Typed as MyCustomViewController\n```\n\n## Type-safe Associated Object\n\n```swift\n// Define your associate keys\nextension YourClass.AssociateKeys {\n    static let someKey: Key\u003cInt\u003e = \"someKey\"\n}\n\n// Use it!\nyourObject[.someKey] = 42\n```\n\n## License\n\nGenericID is available under the MIT license. See the [LICENSE file](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddddxxx%2Fgenericid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fddddxxx%2Fgenericid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddddxxx%2Fgenericid/lists"}