[初心者向け/iOS開発/Swift構文]
(http://qiita.com/kayo_h/items/5250831b4fe3fb25114f)
構文
基本
// コメント
/* コメント */
var upperCammel = 100 //変数はvar
var variable: Int = 1
upperCammel = 0 // => 0
let varDouble = 5.0 //定数はlet
let constVar = 100
constVar = 0 // => error:Cannnot assign to 'let' value 'constVar'
/* 真偽値 */
var aFlag : Bool = false
- 命名規則:upperCamel
- 文末セミコロンは1行に2文書くときだけ使う
- 文字列オブジェクトに文字列を追加するには+=演算子を使う
- 要素追加も+=演算子
- 暗黙の型変換なし
optional
/* Optional値 */
var n0: Optional<Int>
var n0: Int? // => 糖衣構文というらしい
n0 = 5 // 5
n0 = nil // nil => optionalでないとerrorになる
var n1.Int? = 1
n1 + 1 // error => optional<Int>型にint型を代入できない
n1! + 1 // 2 => Forced Unwrapping (nilだと落ちるかもしれないのでnilチェック必須)
var ans = n1!.advancedBy(1) //これも!がないとerrorになる
if let unwrappedStr = n1 {
printlin(n1)
} // 1 => Optional Binding
var testStr: String? //nil
testStr = "hello" // "hello"
testStr?.uppercaseString // "HELLO" (Optional<Str>型)
Forced Unwrapping
- 強制的に unwrap して変数をT型として扱える
Optional Binding - Optional 型の変数の中身が nil でないことを確認すると同時に unwrap を行う(if)
Optional Chaining - 一旦 unwrap した変数の型のメソッドを使用したい場合に使う。型はoptional型になる。
[詳しくは..]
(http://qiita.com/koher/items/c6f446bad54442a28bf4)
文字列
/* 文字列 */
var name = "aaa" //文字列は""で囲む
var constant: String = "Swift"
var name2 = "xxx"
println "bbb" + "ccc" // => bbbccc(改行)
print ("print ¥(name + name2)") // => aaaxxx
countElements(name) // => 3
配列
/* 配列 */
var numbers = ["one","two","three"]
let names = String[]() //空配列 初期化子構文というらしい
numbers[0] = "one"
numbers += "four"
numbers.count // => 4
for numbers in numbers {
pringln(number)
}
dictionary
/* dictionary */
let lang: (String,Int) = ("Swift",0)
lang.0 // => swift
lang.1 // => 0
var hashCounts = ["first": 1, "secont": 2]
var wordCounts = Dictionary<String, Int>() // 空ハッシュ
hashCounts.count // =>2
hashCounts.keys
hashCounts.values
for (xx, yy) in hashCounts {
println(xx + ":" + String(yy))
}
[dictionary詳しく]
(http://qiita.com/hachinobu/items/d591d0cba23b6dfec559)
if
/* if */
let value = 0.4
func test(value : Double) -> String{
if (value > 0.5) {
return "xx"
} else if value > 0.6 { //()は不要
return "yy"
} else{
return "zz"
}
} // => zz
value > 1 ? "up" : "down" // => down
while系
/* while */
let i = 0
while i < 10 {
println (i)
i++
}
/* do while */
i = 0
do {
println (i)
i = i++
} while i < 10
/* for */
for var i = 0; i < 10; i++ {
println (i)
}
/* for in */
var i = 0
for i in 0..9 {
println (i)
}
[eachが恋しい人は。。]
(http://qiita.com/kaiinui/items/cc8621ef72890cd25e18)
関数
/* 関数 */
func hogeHoge(x: Int) -> Int { return x }
func hogeHoge(x: String ) -> void { println(x) }
func hogeHoge(x: String ) { println(x) }
/* 複数返値 */
func piyo() -> (Unt,String){retrun(1,"message")}
/* 可変長 */
func piyo(name:String,) -> String {return name[]%length }
// TODO:調べ直し
/* クロージャー */
let hoge = {( x:Int -> Int in)// 1行だとreturnは省略可
// TODO:調べ直し
let hoge: Int -> = {x in 2 * x}
let hoge: Int -> = { 2* $0 } // $0,$1..と引数に名前が付いている
[クロージャー]
(http://qiita.com/tajihiro/items/332fe94a25209f1e80c1)
Range
/* 配列にして使う */
let array = (Array<Int>)(1..100)
//あとは自由にArrayとして操作する
println array.map{ $0 * 10 }
その他
/* joinもあった! */
let cells = ["one", "two", "three"]
let csvRow = join(",", cells)
// "one,two,three"
/* splitもあった! */
let csvRow = "one,two,three"
let cells = split(csvRow, { $0 == "," })
// ["one","two","three"]
プロトコル/getter/setter
これいい
http://dev.eyewhale.com/archives/1192
[didset/willset]
(http://qiita.com/takabosoft/items/b3af8b30a8c5111f4fce)
クロージャー
参考
[既存のObjective-CアプリケーションをSwiftで書き換えた話]
(http://techlife.cookpad.com/entry/2015/04/27/180000)
[今すぐObjective-CをやめてSwiftを使おう]
(http://qiita.com/koher/items/4aea541fd0f0d8a9a166)
memo
Objective-C参考
http://d.hatena.ne.jp/glass-_-onion/20080910/1221057977