開発メモ

開発関係のメモをいろいろと。たぶん。

The Swift Programming Languageのメモ(その3)

はじめに

これは、『The Swift Programming Language』を読んだ時の個人的なメモです。なので、引用はすべて『The Swift Programming Language』から。

iTunes - ブック - Apple Inc.「The Swift Programming Language」
https://itunes.apple.com/jp/book/swift-programming-language/id881256329?mt=11

その2はこちら。

The Swift Programming Languageのメモ(その2) - 開発メモ
http://seeku.hateblo.jp/entry/2014/06/06/203045


Language Guide

The Basics

  • 整数型: Int
  • 浮動小数点型: Float / Double
  • 二値型: Bool
  • 文字列型: String
  • コレクション: Array / Dictionary

Declaring Constants and Variables

  • 複数の変数を1行でまとめて定義できるよ
var x = 0.0, y = 0.0, z = 0.0

Naming Constants and Variables

  • 変数名にはUnicode文字が使えるよ
    けど、数学の記号や矢印(?)や無効な文字とかは使えないよ。

  • どうしても予約語と同じ変数名が使いたい時は『`』で囲めば使えるよ
    けど、どうしても必要な時以外はやっちゃ駄目だよ。

Printing Constants and Variables

  • println()はNSLogと同じように使えるよ
    メモ: 要確認

Comments

  • 複数行のコメントはネストできるよ
/* this is the start of the first multiline comment
/* this is the second, nested multiline comment */
this is the end of the first multiline comment */

Semicolons

  • 基本的に、『;』は必要ないよ
  • 1行に複数の文を入れる場合は『;』で区切るよ

Integers

  • 8/16/32/64bitの符号付き/符号無し整数もあるよ
    Int8/UInt8って感じの名前になるよ。

Integer Bounds

  • 『min』と『max』で最小値/最大値を取り出せるよ
let minValue = UInt8.min  // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max  // maxValue is equal to 255, and is of type UInt8

Int

  • IntはObjective-Cで言うとNSIntegerだよ
    32bit環境だとInt32、64bit環境だとInt64になるよ。

Floating-Point Numbers

Type Safety and Type Inference

  • 普通に整数型を書くとIntになるよ
let meaningOfLife = 42
// meaningOfLife is inferred to be of type Int
let pi = 3.14159
// pi is inferred to be of type Double
  • 整数型と浮動小数点型を混ぜるとDoubleになるよ
let anotherPi = 3 + 0.14159
// anotherPi is also inferred to be of type Double

Numeric Literals

  • 整数型ではプレフィックスが使えるよ

    • 無し:10進数
    • 0b:2進数
    • 0o:8進数
    • 0x:16進数
  • 数値を見やすくするために、『_』で区切ることが出来るよ

let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1

Integer Conversion

  • 整数型を変換する時は、明示的に変換するよ
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)

Integer and Floating-Point Conversion

  • 浮動小数点型を整数型に変換すると、小数点以下は切り捨てになるよ
let integerPi = Int(pi)
// integerPi equals 3, and is inferred to be of type Int
  • 4.75は4に、-3.9は-3になるよ

Type Aliases

  • 既存の型に別名をつけることが出来るよ
typealias AudioSample = UInt16

Booleans

  • 二値型はBoolだよ
  • 定数としてtrueとfalseが使えるよ
  • 二値型はifの条件式にそのまま書けるよ

Tuples

  • タプルを使うと、複数の値を1つにまとめられるよ
    型がバラバラでも大丈夫だよ。
let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")
  • こんな感じで値を取り出せるよ
let (statusCode, statusMessage) = http404Error
println("The status code is \(statusCode)")
// prints "The status code is 404"
println("The status message is \(statusMessage)")
// prints "The status message is Not Found
  • 使わない値は『_』で捨てることが出来るよ
let (justTheStatusCode, _) = http404Error
println("The status code is \(justTheStatusCode)")
// prints "The status code is 404
  • インデックスを使ってアクセスすることも出来るよ
println("The status code is \(http404Error.0)")
// prints "The status code is 404"
println("The status message is \(http404Error.1)")
// prints "The status message is Not Found"
  • それぞれの要素に名前をつけることも出来るよ
let http200Status = (statusCode: 200, description: "OK")

println("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"
println("The status message is \(http200Status.description)")
// prints "The status message is OK

Optionals

  • オプション変数は次のどちらかの状態を持つよ

    • 値を持ってて、それはxである
    • 値を持ってない
  • NOTE: Objective-Cnilに似てるけど違うよ。Objective-Cnilはオブジェクトにしか使えないよ。Objective-Cでは、値の有無を示すのに特別な値を使ってたよ(NSNotFoundとか)。Swiftのオプション変数は、すべての型で使えるよ。特別な値も不要だよ。

  • 例えばStringのtoIntの場合、StringをIntに変換するよ
    けど、文字列は整数に変換できないこともあるよ。

let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int
  • だから、toIntはオプション変数のIntを返すよ
    メモ: つまり、変換できなかったらnilになる

メモ: オプション変数は、SQLで言うところのNULL許容みたいな感じか

If Statements and Forced Unwrapping

  • オプション変数に値が入ってる事を確信してる場合、『!』を付ければ値を取り出せるよ
if convertedNumber != nil {
    println("\(possibleNumber) has an integer value of \(convertedNumber!)")
} else {
    println("\(possibleNumber) could not be converted to an integer")
}
// prints "123 has an integer value of 123"
  • もちろん、値が入ってないオプション変数に『!』を使うとランタイムエラーになるよ

Optional Binding

if let actualNumber = possibleNumber.toInt() {
    println("\(possibleNumber) has an integer value of \(actualNumber)")
} else {
    println("\(possibleNumber) could not be converted to an integer")
}
// prints "123 has an integer value of 123

nil

  • オプション変数には、値がない状態としてnilをセットすることが出来るよ
  • nilが一緒に使えるのはオプション変数だけだよ
  • オプション変数を初期値無しで定義すると、nilになるよ
var surveyAnswer: String?
// surveyAnswer is automatically set to nil

Debugging with Assertions

  • assertが使えるよ
let age = -3
assert(age >= 0, "A person's age cannot be less than zero")
// this causes the assertion to trigger, because age is not >= 0

メモ: Release/Debugの切り替えはどうなる?


とりあえずここまで。
(2014/06/07)
(2014/09/25)