Objective-C Survives
       Shumpei Akai
Objective-C

• Objective-C == C + (Smalltalk
    )OOP

• C++ == C + Chaos
Objective-C
•   C

    •   Objective-C++

•
    •       Ruby

    •                   OK

•
•           3

    •
    •
    •
•       C
• Objective-C
•               Cocoa
•
    • gcc
    • clang (LLVM          )

•                   “.m”
•
    • mac     XCode

•
    • Cocoa (mac) / Cocoa Touch (iOS)
    • GNUStep (        )
Objective-C
•   1983:          Brad Cox       (C++   )

•   1988: NeXT (              )


•   1995: NeXT

•   1996: Apple    NeXT

•   2001: Mac OS X

•   2007: Objective-C 2.0
•
    //
      [str length]
    //      1
      [array objectAtIndex:0]
    //     2
     [array insertObject: obj atIndex: 2]
•
    • Ruby Lisp
    • “insertObject:atIndex:”
    • “objectAtIndex:”
•
    • stringByAppendingString:
•
    • setObject:forKey:
•
•
    •
•
    •
id :
※
    Objective-C
• ClassName*   OK

 •
• id
 •
nil
•               (id )

    • null
• nil
 • 0/NULL/nil
 •
NSMutableArray *array =
[NSMutableArray arrayWithObjects: @"one",
@"two”, nil];
NSArray *newAdditions =
[NSArray arrayWithObjects: @"3", @"4", @"5",
nil];

[arry addObjectsFromArray: newAdditions];
BOOL

•
    • C++/C99
•               YES/NO
•                  (.h)
@interface ClassName : SuperClassName {
  id instanceVariable;
}
- (void)instanceMethod;
+ (id)classmethod;
@end
•           (.m)
    @implementation ClassName
    - (void)instanceMethod{
      //
    }

    + (id)classmethod{
      //
    }
    @end
•               (java.lang.Object
      )

    •
    • Cocoa   NSObject

    •
•   java

    •
    •                           2   prefix

    •   apple   NS (== NeXT Step)

    •
•   - (void)instanceMethodWithArg:(id)arg{ }

    •                    Java

    •             self

        •
• obj->instanceVar
 •            (        )

 • self
• @public, @protected, @private
 • @protected               (     )
•   + (void)classMethodWithArg:(id)arg{ }

    •
•               [ClassName methodName]
•   [ClassName class]

    •

    •                   ==



    •   [ClassName foo] == [[ClassName class] foo]
•
    • static
•
+alloc        +allocWithZone:

•
    • Cocoa
    •
    •
- init
    • [[NSObject alloc] init]
       •
       • init
-(id) init{
    self = [super init];
    if(self){
       //
    }
    return self;
}
alloc   init
•
•
•                      ?

    •

    •
Cocoa
•
    •
        • free           retain(+1)

        •            release(-1)

        •        0
•                           caller

          •   alloc,new,copy,mutableCopy

          •                                +1

-(void) doFoo{
  NSString* str=[[NSString alloc] init] ;
  //
    [str release]; //
}
•
        •
- (void)doBarWithArray:(NSArray*)ary{
  id obj=[ary objectAtIndex:0];
  //ary obj release
}
retain
•                    /
        retain

    •

    - (void)doBarWithArray:(NSArray*)ary{
      self->ary = [ary retain];
    }
release
•                       release

    • nil
    - (void)didReceiveMemoryWarning{
      [self->data release];
      self->data = nil; //data free
    }
•                caller

          •   caller   free
-(NSString*) makeString{
  return [[NSString alloc] init] ;
   //caller release
}
                       -(NSString*) makeString{
                         return [[[NSString alloc] init] release];
                          //return
                       }
autorelease
      •           release

          •                                             release

          •                                       obj

-(NSString*) makeString{
  return [[[NSString alloc] init] autorelease];
   //
}
AutoreleasePool
    •   autorelease

        •             =>                              release

        •

- (void)foo{
   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
   //do something
   [pool release];
}
dealloc
•                        0

    •                        obj
        release

-(void) dealloc{
  [self->str release];
  [self->ary release];
  [super dealloc];
}
•

    • Cocoa
    •
      •       retain

      •       retain
• Java interface
 • optional                               OK
 @protocol HogeProtocol <SuperProtocol>

 -(BOOL)isCharactor;
 -(BOOL)isDisplay;
 -(void)start;
 -(void)abort;
 -(BOOL)step;
 @optional
 -(void)callbackWithData:(NSData*)data;
 @end
• Java
 • id<FooProtocol> : FooProtocol

 • BarClass<FooProtocol>* : BarClass
    FooProtocol
•

    @implementation ClassName(CategoryName)
    - (void)addingMethod{
      //
    }
    @end
•   obj.proptyName

           •

@interface ClassName : NSObject {
}
@property (nonatomic,retain) id prop;   @implementation ClassName
@end                                    @synthesize prop;
                                        @end
•   NSObject

•                                 ?

    •   - (BOOL)respondsToSelector:(SEL)aSelector
•
    •   - (id)performSelector:(SEL)aSelector
    •   - (id)performSelector:(SEL)aSelector withObject:(id)anObject
    •   - (id)performSelector:(SEL)aSelector withObject:(id)anObject
        withObject:(id)anotherObject
(cont.)

•
    • - (IMP)methodForSelector:(SEL)aSelector
•
    •   Class NSClassFromString(NSString *aClassName);
•
    • NSString
      •            @”hoge”;

    • NSMutableString
• Cocoa       mutable         immutable
• NSArray/NSMutableArray
 •                    :

        •     [NSMutableArray arrayWithObjects:@”one”,
              @”two”,nil]

•   Cocoa

    •   nil
/

•
    • NSDictionary/NSMutableDictionary
•
    • NSSet/NSMutableSet
•   NSValue
    •   C

•   NSNumber
    •
    •                  1

        •   [NSNumber numberWithBool:YES]
        •   [NSNumber numberWithInt:0]
        •   [NSNumber numberWithDouble:1.0]
Objective-C


•C
•
•
  •       C
Objective-C




•           OOP

    •
Objective-C


• OS
 •
   •
   •          /
•
• delegate
• target/action
• ARC(Automatic Reference Counting)
• Blocks
• Grand Central Dispatch

Objective-C Survives

  • 1.
  • 2.
    Objective-C • Objective-C ==C + (Smalltalk )OOP • C++ == C + Chaos
  • 3.
    Objective-C • C • Objective-C++ • • Ruby • OK •
  • 4.
    3 • • • • C
  • 5.
  • 6.
    • gcc • clang (LLVM ) • “.m”
  • 7.
    • mac XCode • • Cocoa (mac) / Cocoa Touch (iOS) • GNUStep ( )
  • 8.
    Objective-C • 1983: Brad Cox (C++ ) • 1988: NeXT ( ) • 1995: NeXT • 1996: Apple NeXT • 2001: Mac OS X • 2007: Objective-C 2.0
  • 10.
    // [str length] // 1 [array objectAtIndex:0] // 2 [array insertObject: obj atIndex: 2]
  • 11.
    • Ruby Lisp • “insertObject:atIndex:” • “objectAtIndex:”
  • 12.
    • stringByAppendingString: • • setObject:forKey: •
  • 13.
    • • •
  • 15.
  • 17.
    Objective-C
  • 18.
    • ClassName* OK • • id •
  • 19.
    nil • (id ) • null • nil • 0/NULL/nil •
  • 20.
    NSMutableArray *array = [NSMutableArrayarrayWithObjects: @"one", @"two”, nil]; NSArray *newAdditions = [NSArray arrayWithObjects: @"3", @"4", @"5", nil]; [arry addObjectsFromArray: newAdditions];
  • 21.
    BOOL • • C++/C99 • YES/NO
  • 22.
    (.h) @interface ClassName : SuperClassName { id instanceVariable; } - (void)instanceMethod; + (id)classmethod; @end
  • 23.
    (.m) @implementation ClassName - (void)instanceMethod{ // } + (id)classmethod{ // } @end
  • 24.
    (java.lang.Object ) • • Cocoa NSObject •
  • 25.
    java • • 2 prefix • apple NS (== NeXT Step) •
  • 26.
    - (void)instanceMethodWithArg:(id)arg{ } • Java • self •
  • 27.
    • obj->instanceVar • ( ) • self • @public, @protected, @private • @protected ( )
  • 28.
    + (void)classMethodWithArg:(id)arg{ } • • [ClassName methodName]
  • 29.
    [ClassName class] • • == • [ClassName foo] == [[ClassName class] foo]
  • 30.
    • static
  • 31.
  • 32.
    +alloc +allocWithZone: • • Cocoa • •
  • 33.
    - init • [[NSObject alloc] init] • • init -(id) init{ self = [super init]; if(self){ // } return self; }
  • 34.
    alloc init • • • ? • •
  • 35.
    Cocoa • • • free retain(+1) • release(-1) • 0
  • 36.
    caller • alloc,new,copy,mutableCopy • +1 -(void) doFoo{ NSString* str=[[NSString alloc] init] ; // [str release]; // }
  • 37.
    • - (void)doBarWithArray:(NSArray*)ary{ id obj=[ary objectAtIndex:0]; //ary obj release }
  • 38.
    retain • / retain • - (void)doBarWithArray:(NSArray*)ary{ self->ary = [ary retain]; }
  • 39.
    release • release • nil - (void)didReceiveMemoryWarning{ [self->data release]; self->data = nil; //data free }
  • 40.
    caller • caller free -(NSString*) makeString{ return [[NSString alloc] init] ; //caller release } -(NSString*) makeString{ return [[[NSString alloc] init] release]; //return }
  • 41.
    autorelease • release • release • obj -(NSString*) makeString{ return [[[NSString alloc] init] autorelease]; // }
  • 42.
    AutoreleasePool • autorelease • => release • - (void)foo{ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; //do something [pool release]; }
  • 43.
    dealloc • 0 • obj release -(void) dealloc{ [self->str release]; [self->ary release]; [super dealloc]; }
  • 44.
    • Cocoa • • retain • retain
  • 45.
    • Java interface • optional OK @protocol HogeProtocol <SuperProtocol> -(BOOL)isCharactor; -(BOOL)isDisplay; -(void)start; -(void)abort; -(BOOL)step; @optional -(void)callbackWithData:(NSData*)data; @end
  • 46.
    • Java •id<FooProtocol> : FooProtocol • BarClass<FooProtocol>* : BarClass FooProtocol
  • 47.
    @implementation ClassName(CategoryName) - (void)addingMethod{ // } @end
  • 48.
    obj.proptyName • @interface ClassName : NSObject { } @property (nonatomic,retain) id prop; @implementation ClassName @end @synthesize prop; @end
  • 49.
    NSObject • ? • - (BOOL)respondsToSelector:(SEL)aSelector • • - (id)performSelector:(SEL)aSelector • - (id)performSelector:(SEL)aSelector withObject:(id)anObject • - (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject
  • 50.
    (cont.) • • - (IMP)methodForSelector:(SEL)aSelector • • Class NSClassFromString(NSString *aClassName);
  • 51.
    • NSString • @”hoge”; • NSMutableString • Cocoa mutable immutable
  • 52.
    • NSArray/NSMutableArray • : • [NSMutableArray arrayWithObjects:@”one”, @”two”,nil] • Cocoa • nil
  • 53.
    / • • NSDictionary/NSMutableDictionary • • NSSet/NSMutableSet
  • 54.
    NSValue • C • NSNumber • • 1 • [NSNumber numberWithBool:YES] • [NSNumber numberWithInt:0] • [NSNumber numberWithDouble:1.0]
  • 55.
  • 56.
  • 57.
  • 58.
    • • delegate • target/action •ARC(Automatic Reference Counting) • Blocks • Grand Central Dispatch