{"id":20879235,"url":"https://github.com/7c/swift-cheatsheet","last_synced_at":"2026-03-17T07:38:21.781Z","repository":{"id":129721208,"uuid":"443421680","full_name":"7c/swift-cheatsheet","owner":"7c","description":"my interpretation of swift cheatsheet","archived":false,"fork":false,"pushed_at":"2022-01-06T10:52:42.000Z","size":263,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-29T00:10:33.559Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/7c.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-12-31T20:35:18.000Z","updated_at":"2022-01-06T10:52:44.000Z","dependencies_parsed_at":"2023-03-15T08:15:13.441Z","dependency_job_id":null,"html_url":"https://github.com/7c/swift-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/7c/swift-cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7c%2Fswift-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7c%2Fswift-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7c%2Fswift-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7c%2Fswift-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/7c","download_url":"https://codeload.github.com/7c/swift-cheatsheet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7c%2Fswift-cheatsheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30617498,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-17T04:46:40.957Z","status":"ssl_error","status_checked_at":"2026-03-17T04:46:32.538Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2024-11-18T07:15:39.927Z","updated_at":"2026-03-17T07:38:21.765Z","avatar_url":"https://github.com/7c.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"[SwiftUI](swiftui.md)\n# Swift Cheatsheet\n\n\n## Enums\n```swift\nenum Taste {\n  case sweet, sour, salty, bitter, umami\n}\nlet vinegarTaste = Taste.sour\n\n//Iterating through an enum class\nenum Food: CaseIterable {\n  case pasta, pizza, hamburger\n}\n\nfor food in Food.allCases {\n  print(food)\n}\n\n// enum with String raw values\nenum Currency: String {\n  case euro = \"EUR\"\n  case dollar = \"USD\"\n  case pound = \"GBP\"\n}\n\n// Print the backing value\nlet euroSymbol = Currency.euro.rawValue\n  print(\"The currency symbol for Euro is \\ (euroSymbol)\"')\n\n// enum with associated values\nenum Content {\n  case empty\n  case text(String)\n  case number(Int)\n}\n\n// Matching enumeration values with a switch statement\nlet content = Content.text(\"Hello\") \nswitch content {\ncase .empty:\n  print(\"Value is empty\")\ncase .text(let value): // Extract the String value\n  print(\"Value is (value)\")\ncase .number(_): // Ignore the Int value\n  print(\"Value is a number\")\n}\n```\n\n## JSON \n### [DynamicJSON](https://github.com/illescasDaniel/DynamicJson)\n### Decoding to Struct\n```swift\n// needs to be Codable\nstruct Account: Codable {\n    let email : String\n    let expiration : Int64\n    let hpassword : String\n    let id : Int\n    let is_test: Int\n    let lastseen : Int64\n    let product_id : String\n    let register_ip : String\n    let uid : String\n    let verified : Int\n    let createdAt : String\n}\n\nif let strdata = (ret[0] as! String).data(using: .utf8) {\n    do {\n        let json = try JSONDecoder().decode(Account.self,from:strdata)\n        print(json)\n    }catch let error {\n        print(error)\n        return\n    }\n}\n\n```\n\n\n## Extension\n```swift\nextension Date {\n    // static extension\n    static func fromNodetime(_ ts:Int) -\u003e Date {\n        return Date(timeIntervalSince1970: Double(ts/1000))\n    }\n    // instanciated extension\n    func yesterday() -\u003e Date? {\n        return self.add(days:-1)\n    }\n}\n```\n\n\n## DateTime\n```swift\nextension Date {\n    // static method\n    static func fromNodetime(_ ts:Int) -\u003e Date {\n        return Date(timeIntervalSince1970: Double(ts/1000))\n    }\n    func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -\u003e Date? {\n            let comp = DateComponents(year: years, month: months, day: days, hour: hours, minute: minutes, second: seconds)\n            return Calendar.current.date(byAdding: comp, to: self)\n    }\n    \n    func yesterday() -\u003e Date? {\n        return self.add(days:-1)\n    }\n}\n\n// yesterdays date\nDate().yesterday()\n// import from nodejs timestamp to swift\nDate.fromNodetime(1640805629848)\n```\n\n## XHR Requests\n### Alamofire\nAlamofire is an HTTP networking library written in Swift. [Features](https://github.com/Alamofire/Alamofire#features), [Docs](https://alamofire.github.io/Alamofire/)\n```swift\nimport Alamofire\n//\n// basic GET\nAF.request(\"https://ip8.com/echo\",method:.get).response { response in\n    debugPrint(response)\n}\n\n// basic GET with querystring - it is same as calling the url with ?test=1\nvar qs:[String:Any]=[\"test\":1]\nAF.request(\"https://ip8.com/echo\",method:.get,parameters:qs ).response { response in\n    debugPrint(response)\n}\n\n// GET with url enccoded parameters inline\nAF.request(\"https://ip8.com/echo\",parameters: [\"key\":\"value2\"]).response { response in\n     debugPrint(response)\n}\n\n\n// post json encoded parameters\nstruct Login : Encodable {\n    let email : String\n    let password : String\n}\nAF.request(\"https://ip8.com/echo\",\n           method:.post,\n           parameters: Login(email:\"user@test.com\",password:\"secret\"),\n           encoder:JSONParameterEncoder.default).response { response in\n    debugPrint(response)\n}\n\n// post url encoded parameters $_POST\nAF.request(\"https://ip8.com/echo\",method:.post,parameters: [\"foo\":[\"bar\"],\"baz\":[\"a\",\"b\"]]).response { response in\n    debugPrint(\"post url encoded parameters\",response)\n}\n\n// POST with headers\nlet headers: HTTPHeaders = [\n    \"Authorization\": \"Basic VXNlcm5hbWU6UGFzc3dvcmQ=\",\n    \"Accept\": \"application/json\"\n]\n\n\nAF.request(\"https://ip8.com/echo\",method:.post,headers:headers).response { response in\n    // Accessing body and statuscode from response\n    if let statuscode = response.response?.statusCode {\n        print(\"Statuscode: \\(statuscode)\")\n        if statuscode==200 {\n            if let data = response.data  {\n                print(\"Body:\")\n                print(String(decoding:data, as: UTF8.self))\n            }\n        }\n        \n    }\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F7c%2Fswift-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F7c%2Fswift-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F7c%2Fswift-cheatsheet/lists"}