{"id":20187517,"url":"https://github.com/wordpress-mobile/swift-style-guide","last_synced_at":"2025-03-03T07:11:37.533Z","repository":{"id":143101915,"uuid":"43750851","full_name":"wordpress-mobile/swift-style-guide","owner":"wordpress-mobile","description":"Swift Style Guide","archived":false,"fork":false,"pushed_at":"2020-10-27T09:56:24.000Z","size":20,"stargazers_count":37,"open_issues_count":2,"forks_count":4,"subscribers_count":10,"default_branch":"trunk","last_synced_at":"2025-01-13T18:23:16.847Z","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/wordpress-mobile.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":"2015-10-06T13:01:19.000Z","updated_at":"2024-12-23T15:18:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"f15f0cf0-7045-4d86-b8c8-e715c20e6dad","html_url":"https://github.com/wordpress-mobile/swift-style-guide","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/wordpress-mobile%2Fswift-style-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wordpress-mobile%2Fswift-style-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wordpress-mobile%2Fswift-style-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wordpress-mobile%2Fswift-style-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wordpress-mobile","download_url":"https://codeload.github.com/wordpress-mobile/swift-style-guide/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241622613,"owners_count":19992504,"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-11-14T03:24:24.797Z","updated_at":"2025-03-03T07:11:37.514Z","avatar_url":"https://github.com/wordpress-mobile.png","language":null,"readme":"# The Swift Style Guide for WordPress Mobile apps\n\nSo you want to write some Swift code for WordPress apps. That’s nice, thanks a\nlot. But before that take some minutes to read some tips that will probably make\neveryone’s life easier.\n\nFirst, read the [Swift API Design\nGuidelines](https://swift.org/documentation/api-design-guidelines/) carefully.\nIt’s not a long document and it establishes a common ground for what good Swift\ncode looks like.\n\nOur approach to building a style guide is to do it incrementally and reach\nconsensus. You can read more about the [process](PROCESS.md).\n\nWhen there is not a rule for something or there’s ambiguity, you’re encouraged\nto create a new issue with a proposal. In the meantime, the [raywenderlich.com\nSwift Style Guide](https://github.com/raywenderlich/swift-style-guide/) is often\na good place to look for reference, but it’s not our official guide.\n\nBesides keeping this document short, we’re trying to organize it in two\nsections: a _must read_ section for rules we can’t automatically enforce, and an\n_all rules_ section as a reference of every other approved rule. You are not\nrequired to read the _all rules_ section, as Xcode will tell you when you break\nthose rules.\n\n## Must read\n\n### Fix all the warnings\n\nWe’re using [SwiftLint](https://github.com/realm/SwiftLint) to enforce as many\nof our rules as we can, so trust the warnings. Don’t commit or merge code with\nwarnings. Our `Release` builds turn warnings into errors, so if you merge a\nwarning you will create a problem for the person doing the release later on.\n\n### Deal with optionals early\n\nDon't allow optional parameters just to return when they're `nil`.\n\n```swift\n// Correct: the return value has a chance to become non-optional.  Reduced complexity.\nfunc process(image: UIImage) -\u003e UIImage {\n    // ...process the image...\n}\nlet processedImage = optionalImage.map(process)\n\n// Wrong: the return value becomes an optional as well.  Added complexity.\nfunc process(image: UIImage?) -\u003e UIImage? {\n    guard let image == image else {\n        return nil\n    }\n\n    // ...process the image...\n}\nlet processedImage = process(optionalImage)\n```\n\n## All the rules\n\n### Trailing new line\n\nFiles should end in **one** empty new line. A missing new line at the end causes\ntrouble with diffs when the last line changes. More than one empty line is\nunnecessary.\n\n### Avoid trailing whitespace\n\nThere’s no need for whitespace at the end of lines and it is often the cause for\nunnecessary noise in diffs.\n\nYou can go to Xcode’s preferences for _Text Editing_ and make sure both\n_Automatically trim trailing whitespace_ and _Including whitespace-only lines\nare enabled_. Xcode misses some cases, but it should help a lot.\n\n### Avoid semicolons\n\nSwift does not require a semicolon after each statement in your code. There’s no\nreason to add them.\n\n### Colon spacing\n\nColons always have no space on the left and one space on the right when\nspecifying a type.\n\n```swift\n// Correct\nlet name: String\nstruct MyType: MyProtocol {}\nfunc prompt\u003cT: UIViewController\u003e(t: T) where T: Confirmable {}\n\n// Wrong\nlet name : String\nstruct MyType : MyProtocol {}\nfunc prompt\u003cT : UIViewController\u003e(t: T) where T : Confirmable {}\n```\n\n### Braces\n\nOpening braces should be preceded by a single space and on the same line as the declaration. The exception to this rule is when enclosed within parentheses: no preceding space is required.\n\n``` swift\n// Correct\nif let myString = myString {\n    print(myString)\n}\n\nstruct Foo {\n    let bar: String\n}\n\nfoo.map({ print($0) })\n\n// Wrong\nif let myString = myString\n{\n    print(myString)\n}\n\nstruct Foo\n{\n    let bar: String\n}\n```\n\nClosing braces should always be placed on a new line regardless of the number of enclosed statements.\n\n```swift\n// Correct\nguard condition else {\n   return\n}\n\n// Correct\nif condition { \n\n} else {\n\n}\n\n// Wrong\nguard condition else { return }\n\n// Wrong\nif condition { } else { }\n```\n\n### Commas\n\nThere should be no space before a comma and a single one after any comma.\n\n**Preferred:**\n```swift\nfunction print(name: String, surname: String) {\n  ...\n}\n```\n\n**Not Preferred:**\n```swift\nfunction print(name: String ,surname: String) {\n  ...\n}\n```\n\n## Parentheses\n\nParentheses around conditionals, if, for, while, do statements, are not required and should be omitted.\n\n**Preferred:**\n```swift\nif name == \"Hello\" {\n  print(\"World\")\n}\n```\n\n**Not Preferred:**\n```swift\nif (name == \"Hello\") {\n  print(\"World\")\n}\n```\n\n### Forced downcasts and unwrapping\n\nAvoid using `as!` to force a downcast, or `!` to force unwrapping. Use `as?` to attempt the cast, then deal with the failure case explicitly.\n\n``` swift\n// Correct\nfunc process(someObject: Any) {\n    guard let element = someObject as? Element else {\n        // Optional error handling goes here\n        return\n    }\n    process(element)\n}\n\n// Wrong\nfunc process(someObject: Any) {\n    process(someObject as! Element)\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwordpress-mobile%2Fswift-style-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwordpress-mobile%2Fswift-style-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwordpress-mobile%2Fswift-style-guide/lists"}