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

Post on 27-Aug-2014

4.258 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Swift のイケてるとこ

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

• No more YES / NO !

• No more [brackets forMethodCall] !

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

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

I love this one.

Objective-C だとこうなってた

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

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!

イケてないとこ

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

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

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

その2. class-var がない

その2. class-var がない

“not yet” supported とな

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

その2. class-var がない

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

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

その2. class-var がない

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

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

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

その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.") }

その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.") }

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

その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

その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

その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

その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

こうしろということか…

その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

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

その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

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

その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)

順番は変えられない…

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

その5. UIKit binding が中途半端

その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!) …

その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)

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

その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

しかしこれは…

その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 してるようにしか見えない…

その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

こうして頂きたかった。

その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! …

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

その5. UIKit binding が中途半端

import UIKit !let tableView = UIKit.UITableView()

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

その5. UIKit binding が中途半端

import UIKit !let tableView = UIKit.UITableView()

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

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

7/8 beta3 で改善!

その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.

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

あれ?

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

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

append / insert / remove はできない…

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

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

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

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

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

なぜか変えれる

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

その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

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

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 じゃないの?

その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] !?

その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"]

その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.(開き直り)

その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]

その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]

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

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

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

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

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

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

Thanks!

top related