swift を振り返ってみよう #cswift

55
EZNET 熊友宏 http://eznet.jp/ Swift を振り返ってみよう 2015.06.06 カジュアル Swift プログラミング #0

Upload: tomohiro-kumagai

Post on 25-Jul-2015

532 views

Category:

Engineering


1 download

TRANSCRIPT

1. EZ-NET http://ez-net.jp/ Swift 2015.06.06 Swift #0 2. Swift 3. http://ez-net.jp/ @es_kumagai Xcode 5 IP Phone with MOSA 4. Swift 2014.06.02 @ WWDC 2014 5. 1. Fast 2. Modern 3. Safe 4. Interactive 6. Objective-C without the C C 7. 1. Closures 2. Type inference 3. Multiple return types 4. Generics 5. Namespaces 8. 1. Interoperability Swift Objective-C Cocoa API Swift 2. Mix and Match Objective-C Swift 3. Migration Objective-C Swift Swift 9. Swift 10. 1. Fast Objective-C 11. 2. Modern NSArray *array = @[ @"A", @"B", @"C" ]; NSString *string = [array componentsJoinedByString:@", "]; NSLog(@"Value: %@", string); Objective-C let array = [ "A", "B", "C" ] let string = join(", ", array) println("Value: (string)") Swift 12. nil switch 3. Safe 13. 4. Interactive Xcode Playground #!/usr/bin/swift 14. Objective-C without the C 15. i += 10; Objective-C i += 10 Swift 16. if (i == 10) { } Objective-C if i == 10 { } Swift 17. Int enum 18. C NSString *str = @"OBJC STRING"; NSNumber *num = @10; char* str = "C STRING"; int num = 10; Objective-C let str:String = "SWIFT STRING" let num:Int = 10 Swift C 19. NSArray *arr = @[ @5, @10 ]; NSDictionary *dic = @{ @"K1":@1, @"K2":@2 }; let arr:[Int] = [ 5, 10 ] let dic:[String:Int] = [ "K1": 1, "K2": 2 ] Swift Objective-C "@" 20. NSInteger i = 10; const NSInteger i = 10; NSString* s = @"TEXT"; NSMutableString* s = [@"TEXT" mutableCopy]; Objective-C var i = 10 let i = 10 let s = "TEXT" var s = "TEXT" Swift 21. NSString *str = [NSString stringWithFormat:@"Name=%@, Value=%d", name, value]; Objective-C let str:String = "Name=(name), Value=(value)" Swift 22. NSString *str = [@"MSM" stringByAppendingString:@"2014"]; Objective-C let str:String = "MSM" + "2014" Swift 23. if ([string1 isEqualToString:string2]) { } Objective-C if string1 == string2 { } Swift 24. [MyClass methodWithValue:10.0 ofType:@"$"] Objective-C MyClass.method(value:10.0, ofType:"$") Swift 25. // let value:(Int,String) = (200, "SWIFT") // nil let value:Int? = nil // enum Enumerate { case Name(String) case NoName } Swift 26. switch 27. let dec = 200 // 10 let hex = 0xc8 // 16 let oct = 0o310 // 8 let bin = 0b11001000 // 2 let d = 2.10 let d = 1.25e-4 // 1.2510-4 let d = 0x15p3 // 0x1523 28. Objective-C without the C 29. C Objective-C without the C 30. Objective-C C Swift 31. Swift 32. Closure // let isOK:(Int)->Bool = {(code:Int)->Bool in return contains(200..(code:Int, status:String) { return (200, "OK") } Swift enum Status { case OK case Failed(String) } func getStatus()->Status { return Status.OK } Swift 35. Generics func add(v1:T, v2:T)->T { return value1 + value2 } Swift 36. Namespaces import MyModule1 import MyModule2 let obj1 = MyModule1.MyClass() let obj2 = MyModule2.MyClass() Swift Swift 37. Objective-C 38. Interoperability Objective-C Swift 39. Interoperability Swift Objective-C Swift Objective-C NSObject @objc Objective-C @objc Objective-C , Generics, 40. Interoperability Swift Objective-C Swift Objective-C MyClass* obj = [[MyClass alloc] initWithValue:10]; obj.value; class MyClass : NSObject { var value:Int init(value:Int) } Swift Objective-C #import "PROJNAME-Swift.h" 41. Interoperability Swift Objective-C Cocoa API Swift Swift let value:NSString = "TEST STRING" value.stringByReplacingOccurrencesOfString("TEST", withString: "SWIFT", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, value.length)) Swift import Foundation 42. Mix and Match Objective-C Swift Swift Objective-C 43. Mix and Match Swift Objective-C Objective-C Swift 44. Migration Objective-C Swift Swift Objective-C 45. Migration Swift Objective-C Objective-C Swift Build Settings Objective-C Bridging Header Swift #import Swift C++ / Objective-C++ C++ OK Swift import 46. let obj = MyClass(value:10) obj.value Migration Swift Objective-C Objective-C Swift @interface MyClass : NSObject @property (readwrite) NSInteger value; - (instancetype)initWithValue:(NSInteger)value; @end Swift Objective-C 47. Objective-C Swift Swift 48. Swift 49. 50. let var 51. // var location:(x,y) // let location:(x,y) 52. Optional ImplicitlyUnwrappedOptional 53. 54. switch 55. Swift