Swiftを少しだけ

 Swiftを少しだけ

NDS meetup 1 の資料
Swift について Objective-C との文法比較。

F588267d3e7e23475cad3933226c1e96?s=128

Shintaro Abe

July 14, 2014
Tweet

Transcript

  1. NDS meetup #1 2014.07.13 Sun. dictav

  2. Swift “Swift is a new programming language for iOS and

    OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. ” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/jp/jEUH0.l Swift ެࣜ: https://developer.apple.com/swift/
  3. ࡞ऀ • Chris Lattner • Apple Engineer • http://www.nondot.org/sabre/

  4. Objective-C • 1983೥ੜ·ΕʢC++ͱಉ͡ʣ • C ͷεʔύʔηοτ • C++: better C

    • Objective-C: C & Object System • runtime + Foundation framework (+ AppKit)
  5. Objective-C #import <Foundation/Foundation.h> @interface MyObject : NSObject @end @implementation MyObject

    - (void) helloWorld { printf("Hello, World!\n"); } @end int main(int argc, char **argv) { id obj = [MyObject new]; [obj helloWorld]; return 0; }
  6. Swift import Foundation class MyObject { func helloWorld() { println("hello,

    World!") } } let obj = MyObject() obj.helloWorld() exit(0)
  7. ΏΔ͞΋࢒ͭͭ͠

  8. Objective-C @interface NSString (MyMethod) @end @implementation NSString (MyMethod) - (void)url

    { [NSURL URLWithString:self]; } @end
  9. Swift extension NSString { func url() -> NSURL { return

    NSURL(string: self) } }
  10. Cͷढറ͔Βͷղ์

  11. Objective-C char *source = "I love Objective-C"; NSString *string =

    [NSString stringWithUTF8String:source]; NSString *string2 = @"I love Objective-C"; NSNumber *num = @1; for( int n = 0; n < 100; n++) { num = @(num.integerValue + n); } NSArray *array = @[string, string2, num]; NSDictionary *dict = @{ @"str":string, @"str2":string2, @"num":num};
  12. Swift let string = "I love Swift" var num =

    1 for (var n = 0; n < 100; n++) { num += n } let array = [string, num] let dict = ["string":string, "num":num]
  13. ࣌ʹ͸ݫ͘͠

  14. Objective-C @protocol MyProtocol <NSObject> - (void)helloWorld; @end @interface MyObject :

    NSObject <MyProtocol> @end @implementation MyObject - (void) helloWorld { NSLog(@"Hello, World! (TestLib)"); } @end
  15. Objective-C NSMutableArray *array = [NSMutableArray new]; [array addObject:[MyObject new]]; [array

    addObject:[NSObject new]]; // ΤϥʔʹͳΒͳ͍ʂ [array enumerateObjectsUsingBlock:^(id<MyProtocol> obj, NSUInteger idx, BOOL *stop) { [obj helloWorld];// ͜͜Ͱ࣮ߦ࣌Τϥʔʂ }]; MyObject *objects[2]; objects[0] = [MyObject new]; objects[1] = [NSObject new]; // warning
  16. Swift protocol MyProtocol { func helloWorld() } class MyObject :

    MyProtocol { func helloWorld() { println("hello,world") } } var objects = Array<MyObject>() objects.append(MyObject()) objects.append(NSObject()) // ίϯύΠϧΤϥʔ
  17. มΘΒ͵GCD

  18. Objective-C dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ NSLog(@"hello"); });

    NSOperationQueue *opQueue = [NSOperationQueue new]; [opQueue addOperationWithBlock:^{ NSLog(@"hello"); }];
  19. Swift let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) dispatch_async(queue){ println("hello") } let

    opQueue = NSOperationQueue(); opQueue.addOperationWithBlock{ println("hello") }
  20. Cͱ΋஥ྑ͠

  21. Objective-C #import <stdio.h>

  22. Swift -FUT(P4XJGU#SJEHJOH)FBEFSI /* Go ͸ίϝϯτͰॻ͍ͨΓ͠·͢Ͷ // #include <stdio.h> // #include

    <errno.h> import "C" */ #import <stdio.h>
  23. Swift͸ίϚϯυԽͰ͖Δ

  24. Swift #!/Applications/Xcode6-Beta3.app/Contents/Developer/ usr/bin/xcrun swift -i println("hello")

  25. ͦ͏͍΍ try-catch ͳ͍ͳ

  26. ୭͕Swift࢖͏ͷʁ • ΧδϡΞϧͳήʔϜϓϩάϥϛϯά • Objective-Cʹৄ͍͠ઌୡ

  27. stackoverflow