Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swiftを少しだけ

 Swiftを少しだけ

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

Shintaro Abe

July 14, 2014
Tweet

More Decks by Shintaro Abe

Other Decks in Programming

Transcript

  1. 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/
  2. Objective-C • 1983೥ੜ·ΕʢC++ͱಉ͡ʣ • C ͷεʔύʔηοτ • C++: better C

    • Objective-C: C & Object System • runtime + Foundation framework (+ AppKit)
  3. 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; }
  4. Swift import Foundation class MyObject { func helloWorld() { println("hello,

    World!") } } let obj = MyObject() obj.helloWorld() exit(0)
  5. 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};
  6. 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]
  7. Objective-C @protocol MyProtocol <NSObject> - (void)helloWorld; @end @interface MyObject :

    NSObject <MyProtocol> @end @implementation MyObject - (void) helloWorld { NSLog(@"Hello, World! (TestLib)"); } @end
  8. 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
  9. Swift protocol MyProtocol { func helloWorld() } class MyObject :

    MyProtocol { func helloWorld() { println("hello,world") } } var objects = Array<MyObject>() objects.append(MyObject()) objects.append(NSObject()) // ίϯύΠϧΤϥʔ
  10. 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"); }];