間距 Spacing

  • control structures後需加一個空白。
  • 左小括弧後不加空白。
  • 右小括弧前不加空白。
  • 右小括弧及大佐括弧間需加一個空白。
  • control structures中的程式必須縮進一次。
  • 結尾的左大括弧必須為獨立一行。
var square = 0
var diceRoll = 0

while square < finalSquare {
    // 擲骰子
    if ++diceRoll == 7 { 
        diceRoll = 1
     }
    // 根據點數移動
    square += diceRoll
    if square < board.count {
        // 如果玩家還在棋盤上,順著梯子爬上去或者順著蛇滑下去
        square += board[square]
    }
}

行數 Lines

  • 每行80個字左右,最多不要超過120個字。
// 超過的話,處理如下。

if results.next() {
    movieInfo = MovieInfo(movieID: Int(results.int(forColumn: field_MovieID)),
                          title: results.string(forColumn: field_MovieTitle),
                          category: results.string(forColumn: field_MovieCategory),
                          year: Int(results.int(forColumn: field_MovieYear)),
                          movieURL: results.string(forColumn: field_MovieURL),
                          coverURL: results.string(forColumn: field_MovieCoverURL),
                          watched: results.bool(forColumn: field_MovieWatched),
                          likes: Int(results.int(forColumn: field_MovieLikes))
    )

}
else {
    print(database.lastError())
}
  • 空白行能幫助辨識有關聯的程式區塊。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    let currentMovie = movies[indexPath.row]

    cell.textLabel?.text = currentMovie.title
    cell.imageView?.contentMode = UIViewContentMode.scaleAspectFit

    (URLSession(configuration: URLSessionConfiguration.default)).dataTask(with: URL(string: currentMovie.coverURL)!, completionHandler: { (imageData, response, error) in
        if let data = imageData {
            DispatchQueue.main.async {
                cell.imageView?.image = UIImage(data: data)
                cell.layoutSubviews()
            }
        }
    }).resume()

    return cell
}

results matching ""

    No results matching ""