參考 Apple 官方 Swift 聖經的程式空白縮排格式

為了寫出更好更漂亮的 Swift 程式,就算是小小的空白也要隨時記在心上。以下是彼得潘參考 Apple 官方聖經 The Swift Programming Language 整理的小小幾點空白注意事項。


: 後留一個空白,比方 Type Annotation,Inheritance 和 dictionary。

let name: String = "彼得潘"class ViewController: UIViewController {}let songDic: [String: String] = ["singer": "田馥甄", "name": "演員"]

逗號後留一個空白,比方分隔參數,分隔 array 成員,遵從 protocol。

func crushOn(name: String, gender: String) {}crushOn(name: "Wendy", gender: "女")class ViewController: UIViewController, UITableViewDelegate {}let names = ["Peter", "Wendy", "Hook"]

-> 的前後留一個空白

func crushOn(name: String, gender: String) -> Bool {    if name == "Wendy" && gender == "女" {        return true    } else {        return false    }}

{ 前留一個空白,比方類別的 {,function 的 {

class Baby {}

else 接在 } 的後面,中間留一個空白

var age = 18if age < 30 {print("你是我的傳說")} else if age < 50 {print("你可能是我的傳說")} else {print("你不能是我的傳說")}

彼得潘的 Swift iOS App 開發問題解答集

彼得潘和學生們在開發 iOS App 路上曾經解決的問題集

彼得潘的 iOS App Neverland

Written by

彼得潘的 Swift 程式設計入門,App程式設計入門作者,彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,http://apppeterpan.strikingly.com

彼得潘的 Swift iOS App 開發問題解答集

彼得潘和學生們在開發 iOS App 路上曾經解決的問題集

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade