{"id":16853163,"url":"https://github.com/devxoul/slots","last_synced_at":"2025-04-11T07:11:19.332Z","repository":{"id":25977056,"uuid":"29419124","full_name":"devxoul/Slots","owner":"devxoul","description":"Dynamic contents management for Swift.","archived":false,"fork":false,"pushed_at":"2016-12-12T09:36:34.000Z","size":47,"stargazers_count":9,"open_issues_count":1,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-25T04:52:04.835Z","etag":null,"topics":[],"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/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":"2015-01-18T07:31:00.000Z","updated_at":"2019-11-18T04:01:55.000Z","dependencies_parsed_at":"2022-08-24T14:18:36.847Z","dependency_job_id":null,"html_url":"https://github.com/devxoul/Slots","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSlots","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSlots/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSlots/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devxoul%2FSlots/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devxoul","download_url":"https://codeload.github.com/devxoul/Slots/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248358599,"owners_count":21090404,"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:50:00.019Z","updated_at":"2025-04-11T07:11:19.300Z","avatar_url":"https://github.com/devxoul.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"Slots\n=====\n\n![Swift](https://img.shields.io/badge/Swift-3.0-orange.svg)\n[![Build Status](https://travis-ci.org/devxoul/Slots.svg)](https://travis-ci.org/devxoul/Slots)\n[![CocoaPods](http://img.shields.io/cocoapods/v/Slots.svg?style=flat)](http://cocoapods.org/?q=name%3ASlots%20author%3Adevxoul)\n\nDynamic contents management for Swift.\n\n\nAt a Glance\n-----------\n\n```swift\nlet slots = Slots()\nslots.pattern = [\"Month\", \"Picture\", \"Picture\", \"Month\", \"Picture\"]\nslots[\"Month\"] = [\"Nov 2014\", \"Dec 2014\", \"Jan 2015\"]\nslots[\"Picture\"] = [Picture(1), Picture(2), Picture(3)]\n```\n\nThen:\n\n```swift\nslots.contents // \"Nov 2014\", Picture(1), Picture(2), \"Dec 2014\", Picture(3), \"Jan 2015\"\nslots[2] // Picture(2)\nslots.type(at: 3) // \"Month\"\n```\n\n\nInstallation\n------------\n\n- **For iOS 8+ projects** with [CocoaPods](https://cocoapods.org):\n\n    ```ruby\n    pod 'Slots'\n    ```\n\n- **For iOS 8+ projects** with [Carthage](https://github.com/Carthage/Carthage):\n\n    ```\n    github \"devxoul/Slots\" ~\u003e 2.0\n    ```\n\n- **For iOS 7 projects** with [CocoaSeeds](https://github.com/devxoul/CocoaSeeds):\n\n    ```ruby\n    github 'devxoul/Then', '2.0.0', :files =\u003e 'Slots/*.swift'\n    ```\n\n\nGetting Started\n---------------\n\n### Storing Contents\n\nSlots can store all kind of contents. Each type of contents must be an unique string value.\n\nThis code below describes a basic example for storing some alphabet letters and numbers:\n\n```swift\nlet slots = Slots()\nslots[\"alphabet\"] = [\"a\", \"b\", \"c\"]\nslots[\"number\"] = [1, 2, 3, 4, 5]\n```\n\n\n### Using Pattern\n\nSlots can sort stored contents according to specified pattern. You can assign array of content types to `pattern` property to specify contents pattern.\n\nFor example, when contents pattern is set to `[\"alphabet\", \"number\"]`, Slots will sort contents of `\"alphabet\"` and those of `\"number\"` to be alternated with each other.\n\n```swift\nslots.pattern = [\"alphabet\", \"number\"]\nslots[\"alphabet\"] = [\"a\", \"b\", \"c\"]\nslots[\"number\"] = [1, 2, 3, 4, 5]\nslots.contents // [\"a\", 1, \"b\", 2, \"c\", 3]\n```\n\nAlthough there are 5 objects in `\"alphabet\"`, but `slots.contents` returns array that contains only 3 numbers because there is no more objects in `\"alphabet\"` after 3 time alternated. You can check the number of elements in sorted contents with `count` property.\n\n```swift\nslots.count // 6\n```\n\nSame content type can appear more than once.\n\n```swift\nslots.pattern = [\"alphabet\", \"number\", \"number\"]\nslots.contents // [\"a\", 1, 2, \"b\", 3, 4, \"c\", 5]\n```\n\n\n### Repeatables\n\nYou can assign repeatable content types to `repeatables` property. Content types specified in `repeatables` will be repeated until the contents are all exhausted.\n\n```swift\nslots.pattern = [\"alphabet\", \"number\"]\nslots.repeatables = [\"number\"] // make repeatable\nslots[\"alphabet\"] = [\"a\", \"b\", \"c\"]\nslots[\"number\"] = [1, 2, 3, 4, 5]\nslots.contents // [\"a\", 1, \"b\", 2, \"c\", 3, 4, 5]\n```\n\n\n### Getting Contents\n\nSlots provides the easy way to get contents. Let's assume that the slots declared like this:\n\n```swift\nslots.pattern = [\"alphabet\", \"number\", \"number\"]\nslots[\"alphabet\"] = [\"a\", \"b\", \"c\"]\nslots[\"number\"] = [1, 2, 3, 4, 5]\n```\n\nThen we can get 7th content in the slots with subscription, like an array.\n\n```swift\nslots[6] // \"c\"\n```\n\nIf we attempt to get value of non-existing index, it'll return `nil`.\n\n```swift\nslots[6] // \"c\"\nslots[7] // 5\nslots[8] // nil\n```\n\nWe can get `Slice` with subrange.\n\n```swift\nslots[0..\u003c4] // \"a\", 1, 2, \"b\"\n```\n\n\n### Real-World Example\n\nLet's apply Slots to real world situation. Assume that we have to make a newsfeed like [StyleShare](https://stylesha.re)'s stylefeed. There are many content types in a feed, such as: style, featured user, featured collection, advertisements, etc.\n\n```swift\n// declare content types as constant\nstruct ContentType {\n    static let style = \"style\"\n    static let advertisement = \"advertisement\"\n}\n\nfunc viewDidLoad() {\n    super.viewDidLoad()\n\n    // each advertisements appear after 3 styles\n    self.slots.pattern = [\n        ContentType.style, ContentType.style, ContentType.style\n        ContentType.advertisement\n    ]\n    \n    // styles must be repeated even if there's no advertisements.\n    self.slots.repeatables = [ContentType.style]\n}\n\nfunc fetchStyles() {\n    self.slots[ContentType.style] = // styles from API response\n    self.tableView.reloadData()\n}\n\nfunc fetchAdvertisements() {\n    self.slots[ContentType.advertisement] = // advertisements from API response\n    self.tableView.reloadData()\n}\n\nfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -\u003e Int {\n    return self.slots.count\n}\n\nfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -\u003e UITableViewCell {\n    let contentType = self.slots.type(at: indexPath.row)\n    let content = self.slots[indexPath.row]\n    \n    switch contentType {\n        case ContentType.style:\n            let cell = // ...\n            cell.style = content as Style\n            return cell\n\n        case ContentType.advertisement:\n            let cell = // ...\n            cell.advertisement = content as Advertisement\n            return cell\n    }\n}\n```\n\n\nLicense\n-------\n\nSlots is under MIT license. See the [LICENSE](LICENSE) file for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevxoul%2Fslots","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevxoul%2Fslots","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevxoul%2Fslots/lists"}