swift がいまいちイケてない 10 のコト

54

Upload: taketo-sano

Post on 27-Aug-2014

4.258 views

Category:

Software


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Swift がいまいちイケてない 10 のコト
Page 2: Swift がいまいちイケてない 10 のコト

Swift のイケてるとこ

Page 3: Swift がいまいちイケてない 10 のコト

イケてるとこ• No more too many @s!

• No more YES / NO !

• No more [brackets forMethodCall] !

• Type Inference, Closures, Generics, Operator Overloading, …

Page 4: Swift がいまいちイケてない 10 のコト

let view: UIView = … view.frame.origin.x = 100 // hooray, no error!!!

I love this one.

Page 5: Swift がいまいちイケてない 10 のコト

Objective-C だとこうなってた

UIView *view = … ; view.frame.origin.x = 100;

Page 6: Swift がいまいちイケてない 10 のコト

NOTE !“Unlike Objective-C, Swift enables you to set sub-properties of a structure property directly. ” !Apple Inc. “The Swift Programming Language”。 iBooks. https://itun.es/jp/jEUH0.l

YES!

Page 7: Swift がいまいちイケてない 10 のコト

イケてないとこ

Page 8: Swift がいまいちイケてない 10 のコト

その1. public, private などがない7/22 beta4 で対応!

Page 9: Swift がいまいちイケてない 10 のコト

その1. public, private などがない

class Hoge { public let a = 1 private var b = 2 }

Page 10: Swift がいまいちイケてない 10 のコト

その2. class-var がない

Page 11: Swift がいまいちイケてない 10 のコト

その2. class-var がない

“not yet” supported とな

class Hoge { class let a = 1 class var b = 2 }

Page 12: Swift がいまいちイケてない 10 のコト

その2. class-var がない

※ computed class-property がそれっぽく使える

class Hoge { class var hoge: String { return "hoge" } } !println(Hoge.hoge) // "hoge"

Page 13: Swift がいまいちイケてない 10 のコト

その2. class-var がない

※ struct は、stored / computed 共にいける

struct Hoge { static var hoge = "hoge" static var fuga: String { return "fuga" } } !println(Hoge.hoge) println(Hoge.fuga)

Page 14: Swift がいまいちイケてない 10 のコト

その3. optional-binding の構文がいまいち

Page 15: Swift がいまいちイケてない 10 のコト

その3. optional-binding の構文がいまいち

何やってるか分かります?

let intString = "1024" let maybeInt = intString.toInt() !if let actualInt = maybeInt { println(actualInt) // 1,024 } else { println("\(intString) not an Int expression.") }

Page 16: Swift がいまいちイケてない 10 のコト

その3. optional-binding の構文がいまいち

これと同じ

let intString = "1024" let maybeInt: Int? = intString.toInt() // optional !if maybeInt { let actualInt: Int = maybeInt! // unwrap println(actualInt) // 1,024 } else { println("\(intString) not an Int expression.") }

Page 17: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

Page 18: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

func の場合、引数をカンマ区切りで

func add(a: Int, b: Int, c: Int) -> Int { return a + b + c; } !add(1, 2, 3) // 6

Page 19: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

init の場合、named-parameter を指定する

class Hoge { let result: Int init(a: Int, b: Int, c: Int) { result = a + b + c } } !let hoge = Hoge(a: 1, b: 2, c: 3) hoge.result // 6

Page 20: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

method の場合、 2番目以降の named-parameter を指定する (WTF!?!?!??)

class Hoge { func add(a: Int, b: Int, c: Int) -> Int { return a + b + c; } } !let hoge = Hoge() hoge.add(1, b: 2, c: 3) // 6

Page 21: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

class Hoge { func addWithA(a: Int, b: Int, c: Int) -> Int { return a + b + c; } } !let hoge = Hoge() hoge.addWithA(1, b: 2, c: 3) // 6

こうしろということか…

Page 22: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

class Hoge { func add(#a: Int, b: Int, c: Int) -> Int { return a + b + c; } } !let hoge = Hoge() hoge.add(a: 1, b: 2, c: 3) // 6

※ 最初のパラメータも # をつけて指定必須にできる

Page 23: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

class Hoge { func add(_ a: Int, _ b: Int, _ c: Int) -> Int { return a + b + c; } } !let hoge = Hoge() hoge.add(1, 2, 3) // 6

※ _ をつけて、指定させないようにもできる

Page 24: Swift がいまいちイケてない 10 のコト

その4. func / init / method での named-parameter の違い

class Hoge { func add(#a: Int, b: Int, c: Int) -> Int { return a + b + c; } } !let hoge = Hoge() hoge.add(b: 2, c: 3, a: 1)

順番は変えられない…

Page 25: Swift がいまいちイケてない 10 のコト

Obj-C 先輩への遠慮が伺える

Page 26: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

Page 27: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

initWith… は全部 init に統一されている (これはGOOD!)

class UIColor : NSObject, NSSecureCoding, NSCoding, NSCopying { // Initializers for creating non-autoreleased colors init(white: CGFloat, alpha: CGFloat) init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) init(CGColor cgColor: CGColor!) init(patternImage image: UIImage!) init(CIColor ciColor: CIColor!) …

Page 28: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

let color1 = UIColor(red: 1.0, green: 0.5, blue: 0.8, alpha: 1.0) let color2 = UIColor(hue: 1.0, saturation: 0.25, brightness: 0.5, alpha: 1.0)

インスタンス化がとても直観的

Page 29: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

let color = UIColor(red: 1.0, green: 0.5, blue: 0.8, alpha: 1.0) !var red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; !color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) !println("red: \(red)") // red: 1.0 println(“green: \(green)") // green: 0.5 println("blue: \(blue)") // blue: 0.8 println(“alpha: \(alpha)") // alpha: 1.0

しかしこれは…

Page 30: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

let color = UIColor(red: 1.0, green: 0.5, blue: 0.8, alpha: 1.0) !var red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; !color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) !println("red: \(red)") // red: 1.0 println(“green: \(green)") // green: 0.5 println("blue: \(blue)") // blue: 0.8 println(“alpha: \(alpha)") // alpha: 1.0

getRed してるようにしか見えない…

Page 31: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

let color = UIColor(red: 1.0, green: 0.5, blue: 0.8, alpha: 1.0) !var red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; !color.getComponents(red: &red, green: &green, blue: &blue, alpha: &alpha) !println("red: \(red)") // red: 1.0 println(“green: \(green)") // green: 0.5 println("blue: \(blue)") // blue: 0.8 println(“alpha: \(alpha)") // alpha: 1.0

こうして頂きたかった。

Page 32: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

class NSString { func substringFromIndex(from: Int) -> String! func substringToIndex(to: Int) -> String! func substringWithRange(range: NSRange) -> String! …

From, To, With を一掃して欲しかった。

class NSString { func substring(#from: Int) -> String! func substring(#to: Int) -> String! func substring(#range: NSRange) -> String! …

Page 33: Swift がいまいちイケてない 10 のコト

そういえば名前空間って…

Page 34: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

import UIKit !let tableView = UIKit.UITableView()

うーん、そういうことか…

Page 35: Swift がいまいちイケてない 10 のコト

その5. UIKit binding が中途半端

import UIKit !let tableView = UIKit.UITableView()

こうだったらよかったのに…

Page 36: Swift がいまいちイケてない 10 のコト

その6. let な Array は immutable じゃないの?

7/8 beta3 で改善!

Page 37: Swift がいまいちイケてない 10 のコト

その6. let な Array は immutable じゃないの?

その6. let な Array は immutable じゃないの?

Mutability of Collections !Arrays and dictionaries store multiple values together in a single collection. If you create an array or a dictionary and assign it to a variable, the collection that is created will be mutable. This means that you can change (or mutate) the size of the collection after it is created by adding more items to the collection, or by removing existing items from the ones it already contains. Conversely, if you assign an array or a dictionary to a constant, that array or dictionary is immutable, and its size cannot be changed. !Apple Inc. “The Swift Programming Language”。 iBooks. https://itun.es/jp/jEUH0.l

let は immutable, var は mutable.

Page 38: Swift がいまいちイケてない 10 のコト

その6. let な Array は immutable じゃないの?

あれ?

let array = [1, 2, 3] array[0] = 100 println(array) // [100, 2, 3] ...?

Page 39: Swift がいまいちイケてない 10 のコト

その6. let な Array は immutable じゃないの?

append / insert / remove はできない…

let array = [1, 2, 3] array.append(4) array.insert(0, atIndex: 0) array.removeAtIndex(0)

Page 40: Swift がいまいちイケてない 10 のコト

その6. let な Array は immutable じゃないの?

let な Dictionary は厳密に immutable なのだが…

let dic = ["a": "Apple", "b": "Banana"] !dic["a"] = "Apricot" dic.removeValueForKey("a")

Page 41: Swift がいまいちイケてない 10 のコト

その6. let な Array は immutable じゃないの?

なぜか変えれる

let array = [1, 2, 3] array[0] = 100 println(array) // [100, 2, 3] ...?

Page 42: Swift がいまいちイケてない 10 のコト

その6. let な Array は immutable じゃないの?

えー。

“Immutability has a slightly different meaning for arrays, however. You are still not allowed to perform any action that has the potential to change the size of an immutable array, but you are allowed to set a new value for an existing index in the array. This enables Swift’s Array type to provide optimal performance for array operations when the size of an array is fixed.” !Apple Inc. “The Swift Programming Language”。 iBooks. https://itun.es/jp/jEUH0.l

Page 43: Swift がいまいちイケてない 10 のコト

その7. Array は Struct じゃないの?7/8 beta3 で改善!

Page 44: Swift がいまいちイケてない 10 のコト

Structures and Enumerations Are Value Types !A value type is a type that is copied when it is assigned to a variable or constant, or when it is passed to a function. … All of the basic types in Swift̶integers, floating-point numbers, Booleans, strings, arrays and dictionaries̶are value types, and are implemented as structures behind the scenes. !Apple Inc. “The Swift Programming Language”。 iBooks. https://itun.es/jp/jEUH0.l

Arrays are copied. (三段論法)

その7. Array は Struct じゃないの?

Page 45: Swift がいまいちイケてない 10 のコト

その7. Array は Struct じゃないの?

あれ? コピーされてないやん…

var array1 = [1, 2, 3, 4] var array2 = array1 !array1[0] = 100 !println(array1) // [100, 2, 3, 4] println(array2) // [100, 2, 3, 4] !?

Page 46: Swift がいまいちイケてない 10 のコト

その7. Array は Struct じゃないの?

Dictionary の場合はちゃんとコピーされてる

var dic1 = ["a": "Apple", "b": "Banana"] var dic2 = dic1 !dic1["a"] = "Apricot" !println(dic1) // ["a": "Apricot", "b": "Banana"] println(dic2) // ["a": "Apple", "b": “Banana"]

Page 47: Swift がいまいちイケてない 10 のコト

その7. Array は Struct じゃないの?

Assignment and Copy Behavior for Collection Types

Swift’s Array and Dictionary types are implemented as structures. However, arrays have slightly different copying behavior from dictionaries and other structures when they are assigned to a constant or variable, and when they are passed to a function or method. … For arrays, copying only takes place when you perform an action that has the potential to modify the length of the array. !抜粋:Apple Inc. “The Swift Programming Language”。 iBooks. https://itun.es/jp/jEUH0.l

Arrays are different.(開き直り)

Page 48: Swift がいまいちイケてない 10 のコト

その7. Array は Struct じゃないの?

長さが変わるときだけコピーされる…

var array1 = [1, 2, 3, 4] var array2 = array1 !array1.append(5) !println(array1) // [1, 2, 3, 4, 5] println(array2) // [1, 2, 3, 4]

Page 49: Swift がいまいちイケてない 10 のコト

その7. Array は Struct じゃないの?

unshare() で参照を切り離せる…

var array1 = [1, 2, 3, 4] var array2 = array1 !array1.unshare() array1[0] = 100 !println(array1) // [100, 2, 3, 4] println(array2) // [1, 2, 3, 4]

Page 50: Swift がいまいちイケてない 10 のコト

あと3つ、イケてないってほどでもないけど気になったこと

Page 51: Swift がいまいちイケてない 10 のコト

8. performSelector: 系が一通り使えない

9. KVO がない(今のところ)

10. C++ を直接使えない(C / Obj-C を経由しなきゃいけない)

Page 52: Swift がいまいちイケてない 10 のコト

それ以外のことはだいたいイケてる

Page 53: Swift がいまいちイケてない 10 のコト

今後の進化に期待しましょう

Page 54: Swift がいまいちイケてない 10 のコト

Thanks!