2014年からSwiftを振り返る! #cswift

Preview:

Citation preview

EZ-NET 熊⾕友宏 http://ez-net.jp/

2016.04.23 カジュアル Swift 勉強会 #7

2014年からSwiftを振り返る!Swift カジュアルプログラミング

EZ-NET

iPhone

Swift

EZ-NET

EZ-NET

2014 年 11 ⽉ 1 ⽇

2014 年 11 ⽉ 1 ⽇

MOSA Software Meeting 2014

MSM 2014 当時の資料で今の Swift を振り返ってみよう!

当時の資料

1

79

4

122

⚫︎

⚫︎

⚫︎

⚫︎

⚫︎

⚫︎

⚫︎

NSArray *array = @[ @"A", @"B", @"C" ]; NSString *string = [array componentsJoinedByString:@", "];

NSLog(@"Value: %@", string);

let array = [ "A", "B", "C" ] let string = join(", ", array)

println("Value: \(string)")

⚫︎

⚫︎

⚫︎

⚫︎

8

10

44

⚫︎

⚫︎

1

i += 10;

i += 10

if (i == 10) {

}

if i == 10 {

}

⚫︎

⚫︎

⚫︎

NSString *str = @"OBJC STRING"; NSNumber *num = @10;

char* str = "C STRING"; int num = 10;

let str:String = "SWIFT STRING" let num:Int = 10

17

NSArray *arr = @[ @5, @10 ]; NSDictionary *dic = @{ @"K1":@1, @"K2":@2 };

let arr:[Int] = [ 5, 10 ] let dic:[String:Int] = [ "K1": 1, "K2": 2 ]

24 34

NSInteger i = 10; const NSInteger i = 10;

NSString* s = @"TEXT"; NSMutableString* s = [@"TEXT" mutableCopy];

var i = 10 let i = 10

let s = "TEXT" var s = "TEXT"

3

NSString *str = [NSString stringWithFormat:@"Name=%@, Value=%d", name, value];

let str:String = "Name=\(name), Value=\(value)"

20

NSString *str = [@"MSM" stringByAppendingString:@"2014"];

let str:String = "MSM" + "2014"

18

if ([string1 isEqualToString:string2]) {

}

if string1 == string2 {

}

19

3

[MyClass methodWithValue:10.0 ofType:@"$"]

MyClass.method(value:10.0, ofType:"$")

72 90

// タプル…複数の型を自由に組み合わせられる let value:(Int,String) = (200, "SWIFT")

// nil 許容型…値の他に「ない」状態を扱える let value:Int? = nil

// 値つき列挙型…自由な値を持てる列挙子 enum Enumerate { case Name(String) case NoName }

4

5

8 41 50

44 62 63

⚫︎

⚫︎

⚫︎

⚫︎

⚫︎

64 65 66 67

6

⚫︎

⚫︎

⚫︎

⚫︎

// クロージャの定義 let isOK:(Int)->Bool = {(code:Int)->Bool in

return contains(200..<300, code) }

// 関数のように実行 let result = isOK(200)

// 別の関数の引数に渡して実行 let result = contains(statuses, isOK)

7

79

⚫︎

⚫︎

⚫︎

⚫︎

⚫︎

4 6

31 40

43 44

13

81

8

65

func getStatus()->(code:Int, status:String) { return (200, "OK") }

67

enum Status { case OK case Failed(String) }

func getStatus()->Status { return Status.OK }

50

func add<T:IntegerArithmeticType>(v1:T, v2:T)->T {

return value1 + value2 }

122

⚫︎

⚫︎

⚫︎

⚫︎

9

import MyModule1 import MyModule2

let obj1 = MyModule1.MyClass() let obj2 = MyModule2.MyClass()

⚫︎

⚫︎

⚫︎

⚫︎

⚫︎

⚫︎

MyClass* obj = [[MyClass alloc] initWithValue:10];

obj.value;

class MyClass : NSObject { var value:Int init(value:Int) }

⚫︎

⚫︎

let value:NSString = "TEST STRING"

value.stringByReplacingOccurrencesOfString("TEST", withString: "SWIFT", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, value.length))

⚫︎

⚫︎

⚫︎

let obj = MyClass(value:10) obj.value

@interface MyClass : NSObject

@property (readwrite) NSInteger value; - (instancetype)initWithValue:(NSInteger)value;

@end