{"id":15038542,"url":"https://github.com/melling/swiftcookbook","last_synced_at":"2025-07-08T23:34:13.573Z","repository":{"id":136800156,"uuid":"70159761","full_name":"melling/SwiftCookBook","owner":"melling","description":"Swift Language Cookbook","archived":false,"fork":false,"pushed_at":"2020-10-08T13:48:13.000Z","size":134,"stargazers_count":56,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-24T01:35:13.571Z","etag":null,"topics":["swift-language"],"latest_commit_sha":null,"homepage":"http://www.h4labs.com/dev/ios/swift_cookbook.html","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/melling.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-06T14:05:43.000Z","updated_at":"2024-08-30T11:06:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"6be49b16-171b-4638-92b8-72c1cc2ce297","html_url":"https://github.com/melling/SwiftCookBook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melling%2FSwiftCookBook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melling%2FSwiftCookBook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melling%2FSwiftCookBook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melling%2FSwiftCookBook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/melling","download_url":"https://codeload.github.com/melling/SwiftCookBook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131468,"owners_count":21052819,"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":["swift-language"],"created_at":"2024-09-24T20:38:49.525Z","updated_at":"2025-04-09T23:40:50.517Z","avatar_url":"https://github.com/melling.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# h4labs Swift Cookbook\n\nThis content will (eventually) be mirrored on my site: http://www.h4labs.com/dev/ios/swift_cookbook.html\n\nThis repo will be mainly about the Swift language.  I have another [repo with small iOS projects](https://github.com/melling/ios_topics/blob/master/README.md)\n\nSwift 5.x compatible\n\n- [Basics](README.md) | [Colors](color.md) | [Date and Time](Dates/README.md) | [Sorting](sorting.md) | [Strings](strings.md) | [Functional Swift](functional.md)\n- Collections: [Arrays](array.md) | [Dictionaries](dictionary.md) | [Sets](sets.md) | \n\n## Variable Declarations\n\nCommon types: **Int**, **String**, **Bool**, **CGFloat**, **Double**, **Float**\n\n**let** is used to declare immutable values, while **var** is used to declare mutable variables.\n\n```swift\nlet language = \"Swift\" // Immutable String\nvar i = 0 // mutable Int\ni += 1\nvar x:CGFloat = 2\nvar name:String\nlet max:Int\nmax = 99\nvar isDone = false // Bool\n```\n\n## if\n\n```swift\nlet fuel = 9\nlet msg:String // Must assign in all cases\n\nif fuel \u003c 10 {\n    msg = \"Fuel dangerously low: \\(fuel)\"\n} else if fuel \u003c 50 {\n    msg = \"Fuel low: \\(fuel)\"\n} else {\n    msg = \"Fuel OK\"\n}\n```\n\n## ternary operator\n\n```swift\nlet color = Bool.random() ? \"red\" : \"black\"\n```\n\n## switch\n\n```swift\n\nswitch ticker {\n\n case \"AAPL\":\n\n case \"GOOGL\":\n\n case \"MSFT\":\n\n default:\n\n}\n\n```\n\n## for\n\n```swift\nfor i in 0..\u003c10 {\n    print(\"\\(i)\")\n}\n\nfor i in 0...9 {\n    print(\"\\(i)\")\n}\n```\n\n## for enumeration\n\n```swift\nfor company in [\"Apple\", \"Google\", \"Microsoft\"] {\n    print(\"\\(company)\")\n}\n```\n\n## function declarations\n\n```swift\nfunc add1(i:Int) -\u003e Int { i + 1 } // return word not needed if we have 1 expression\nlet r0 = add1(i: 1) // 2\n```\n\nSame function but add _ to not need a named parameter\n\n```swift\nfunc add1(_ i:Int) -\u003e Int { i + 1 } // return word not needed if we have 1 expression\nlet r0 = add1(1) // 2\n```\n\n\n```swift\n\n```\n### Multiple return values using tuples\n\n```swift\nfunc bestLanguage() -\u003e (String, Int) {\n   return (\"Swift\", 1)\n}\n\nlet (language, rank) = bestLanguage()\n```\n\n## Randomness\n\n```swift\nlet rnd = Int.random(in: 0...1) // 0 or 1\nBool.random() // true or false\n\nInt.random(in: 20...29) // Random in range 20 through 29\nFloat.random(in: 0...1) // Random between 0 and 1\n\n[1,2,3].randomElement() // random Array element\nSet([1,2,3]).randomElement() // random Set element\n```\n\n### Shuffle\n\nSometimes, it's better to shuffle an Array then simply iterate of the result to get a random element.  This allows each element to be chosen at random exactly once.\n\n```swift\nlet shuffled = [1,2,3].shuffled()\n```\n## enum\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelling%2Fswiftcookbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmelling%2Fswiftcookbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelling%2Fswiftcookbook/lists"}