Objective-C


      2011 / 3 / 3

     Kenji Kinukawa
=> k.kinukawa, k_kinukawa
2010     11
    =>
2009
       => Trangram
Apple




Apple
=>Objective-C




                Objective-C
Objective-C
Objective-C
1983       , Stepstone
1985   NeXT Computer
1995   Next       Stepstone    Obj-C
1997   Apple Next             Jobs
       Mac OS X
2007   iPhone                    Obj-C

                                         -wikipedia
iOS                         Cocoa Touch

      iOS
                                                Cocoa
                UIKit                           Touch


            &

                                   Foundation

Core OS                 &
Objective-C
■

■C

■SmallTalk
■

■for, while, if, switch, C
■
■iOS             GC
           malloc free
■
■
    MyUtil.h
               @interface MyUtil{

               ...
               }

               ...
               @end

    MyUtil.m
               @implementation MyUtil

               ...
               @end
■
MyUtil.h


           @interface MyUtil{
            int hoge;

           }

           -(int)sumAB:(int)a adder:(int)b;
           -(int)diffAB:(int)a differ:(int)b;

           @end
■
MyUtil.m

           @implementation MyUtil
           -(int)sumAB:(int)a adder:(int)b
           {
           
 return a+b;
           }

           -(int)diffAB:(int)a differ:(int)b
           {
           
 return a-b;
           }
           @end
■


    -(double)evaluation:(int)val
    {
      hoge;
      huga;
      return buzz;
    }
■id
      id
 id




           id obj;
           [obj msg];
■




NSString * obj;
[obj hogehuga];

warning: 'NSString' may not respond to '-hogehuga'
                   NSString -hogehuga
■


         * hoge =
    [[     alloc] init];
■



    [obj msg];
SICP
(define (fact n)
 (if (= n 1)
   1
   (* (fact (- n 1)) n)))

(define (fact2 n)
 (define (in-fact n ans)
   (if (= n 1)
     ans
     (in-fact (- n 1) (* ans n))))
 (in-fact n 1))

(define (fact3 n)
 (define (fact2-iter n ans)
   (if (= n 0)
     ans
     (fact2-iter (- n 1) (* n ans))))
 (fact2-iter n 1))

(print (fact3 12000))
■




    [obj getHoge];
Obj-C
■

    ※



              -(double)evaluation:(int)val;


                      evaluation:
        -(double)evaluation:(int)val max:(int)a;


                  evaluation:max:
■SEL

SEL
SEL



             [obj callHoge];


  SEL action = @selector(callHoge);
  [obj performSelector:action];
■IMP

Obj-C                           C
                      IMP



IMP funcp = [foo methodForSelector:

             @selector(callHoge)];

xyz =
(*funcp)(foo, @selector(callHoge),nil,nil);
■


    Mac Objective-C2.0   GC



      [obj retain];
      [obj release];
■
    interface




 @protocol MGVoiceClientDelegate<NSObject>
 -(void)mgVoiceClient:(NSURLConnection *)conn   didReceiveResponseError:(NSString *)error;
 -(void)mgVoiceClient:(NSURLConnection *)conn   didFailWithError:(NSError*)error;
 -(void)mgVoiceClient:(NSURLConnection *)conn   didFinishGetting:(NSArray *)voices;
 -(void)mgVoiceClient:(NSURLConnection *)conn   didFinishPosting:(id)reply;
 @end


@interface VoiceTableViewController : UITableViewController <MGVoiceClientDelegate>{

 MGVoiceClient * voiceClient;

 NSArray * voiceArray;
}
■


 Not
             NSString+Parse.h

@interface NSString (Parse)
-(NSDictionary)parseJson:(NSString *)str;
-(NSDictionary)parseXml:(NSString *)str;
-(NSDictionary)parseYaml:(NSString *)str;
@end


[str parseJson:jsonStr];

                            NSString Parse
■
             NSString+Parse.h

@interface NSString (Parse)
-(NSDictionary)parseJson:(NSString *)str;
-(NSDictionary)parseXml:(NSString *)str;
-(NSDictionary)parseYaml:(NSString *)str;
@end


[str parseJson:jsonStr];

                   NSString Parse
■

Cocoa


                   A              B

        method A
                       method B
■


               A   B

    method A




    Obj-C
■


                A                        B

    method A                  method A

               delegate
                          B




    Obj-C
■
@protocol MGVoiceDelegate;

@interface MGVoice : NSObject{
@public

   id <MGVoiceDelegate> delegate;
}
@end

-(void)mgCommentClient:(NSURLConnection *)conn didFinishGetting:(NSArray *)commentArray{

   if([delegate respondsToSelector:@selector(mgVoice:didFinishGettingComments:)]){

   
       [delegate mgVoice:conn didFinishGettingComments:commentArray];

   }
}




- (void)viewDidLoad {
   [super viewDidLoad];

    voice.delegate = self;
}

-(void)mgVoice:(NSURLConnection *)conn didFinishGettingComments:(NSArray *)commentArray{

   if([commentArray count]>0){

   
      MGComment * comment = [commentArray objectAtIndex:0];

   
      commentText.text = comment.commentText;

   }else{

   
      commentText.text = @"                      ";

   }
}
■Blocks
Apple                         C                       ISO
LLVM                              iOS4.0
LLVM Compiler 2.0
LLVM
                                               LLVM      2.0
                                                       C Objective-C   C++




LLVM        GCC       2




Xcode 4                                LLVM   IDE
                                     LLVM
                  Xcode IDE                             C C++ Objective-C




                       block lambda
■Blocks
  void (^b)() = ^{
  
 
 printf("im in blockn");
  };
  b();
■Blocks
    void (^b)() = ^{
    
 
 printf("im in blockn");
    };
    b();

    C
Block
                   Block_copy(),Block_release()
iOS4    Blocks




ex)
  GCD(            )
■

    Objective-C
■
    mixi graph API
    mixi graph API       iPhone

                     ※

Objective-Cひとめぐり

  • 1.
    Objective-C 2011 / 3 / 3 Kenji Kinukawa
  • 2.
    => k.kinukawa, k_kinukawa 2010 11 => 2009 => Trangram
  • 5.
  • 6.
    =>Objective-C Objective-C
  • 7.
  • 8.
  • 9.
    1983 , Stepstone 1985 NeXT Computer 1995 Next Stepstone Obj-C 1997 Apple Next Jobs Mac OS X 2007 iPhone Obj-C -wikipedia
  • 10.
    iOS Cocoa Touch iOS Cocoa UIKit Touch & Foundation Core OS &
  • 11.
  • 12.
    ■ ■for, while, if,switch, C ■ ■iOS GC malloc free
  • 13.
  • 14.
    MyUtil.h @interface MyUtil{ ... } ... @end MyUtil.m @implementation MyUtil ... @end
  • 15.
    ■ MyUtil.h @interface MyUtil{ int hoge; } -(int)sumAB:(int)a adder:(int)b; -(int)diffAB:(int)a differ:(int)b; @end
  • 16.
    ■ MyUtil.m @implementation MyUtil -(int)sumAB:(int)a adder:(int)b { return a+b; } -(int)diffAB:(int)a differ:(int)b { return a-b; } @end
  • 17.
    -(double)evaluation:(int)val { hoge; huga; return buzz; }
  • 18.
    ■id id id id obj; [obj msg];
  • 19.
    ■ NSString * obj; [objhogehuga]; warning: 'NSString' may not respond to '-hogehuga' NSString -hogehuga
  • 20.
    * hoge = [[ alloc] init];
  • 21.
    [obj msg];
  • 22.
    SICP (define (fact n) (if (= n 1) 1 (* (fact (- n 1)) n))) (define (fact2 n) (define (in-fact n ans) (if (= n 1) ans (in-fact (- n 1) (* ans n)))) (in-fact n 1)) (define (fact3 n) (define (fact2-iter n ans) (if (= n 0) ans (fact2-iter (- n 1) (* n ans)))) (fact2-iter n 1)) (print (fact3 12000))
  • 23.
    [obj getHoge];
  • 24.
  • 25.
    ※ -(double)evaluation:(int)val; evaluation: -(double)evaluation:(int)val max:(int)a; evaluation:max:
  • 26.
    ■SEL SEL SEL [obj callHoge]; SEL action = @selector(callHoge); [obj performSelector:action];
  • 27.
    ■IMP Obj-C C IMP IMP funcp = [foo methodForSelector: @selector(callHoge)]; xyz = (*funcp)(foo, @selector(callHoge),nil,nil);
  • 28.
    Mac Objective-C2.0 GC [obj retain]; [obj release];
  • 29.
    interface @protocol MGVoiceClientDelegate<NSObject> -(void)mgVoiceClient:(NSURLConnection *)conn didReceiveResponseError:(NSString *)error; -(void)mgVoiceClient:(NSURLConnection *)conn didFailWithError:(NSError*)error; -(void)mgVoiceClient:(NSURLConnection *)conn didFinishGetting:(NSArray *)voices; -(void)mgVoiceClient:(NSURLConnection *)conn didFinishPosting:(id)reply; @end @interface VoiceTableViewController : UITableViewController <MGVoiceClientDelegate>{ MGVoiceClient * voiceClient; NSArray * voiceArray; }
  • 30.
    ■ Not NSString+Parse.h @interface NSString (Parse) -(NSDictionary)parseJson:(NSString *)str; -(NSDictionary)parseXml:(NSString *)str; -(NSDictionary)parseYaml:(NSString *)str; @end [str parseJson:jsonStr]; NSString Parse
  • 31.
    NSString+Parse.h @interface NSString (Parse) -(NSDictionary)parseJson:(NSString *)str; -(NSDictionary)parseXml:(NSString *)str; -(NSDictionary)parseYaml:(NSString *)str; @end [str parseJson:jsonStr]; NSString Parse
  • 32.
    ■ Cocoa A B method A method B
  • 33.
    A B method A Obj-C
  • 34.
    A B method A method A delegate B Obj-C
  • 35.
    ■ @protocol MGVoiceDelegate; @interface MGVoice: NSObject{ @public id <MGVoiceDelegate> delegate; } @end -(void)mgCommentClient:(NSURLConnection *)conn didFinishGetting:(NSArray *)commentArray{ if([delegate respondsToSelector:@selector(mgVoice:didFinishGettingComments:)]){ [delegate mgVoice:conn didFinishGettingComments:commentArray]; } } - (void)viewDidLoad { [super viewDidLoad]; voice.delegate = self; } -(void)mgVoice:(NSURLConnection *)conn didFinishGettingComments:(NSArray *)commentArray{ if([commentArray count]>0){ MGComment * comment = [commentArray objectAtIndex:0]; commentText.text = comment.commentText; }else{ commentText.text = @" "; } }
  • 36.
    ■Blocks Apple C ISO LLVM iOS4.0 LLVM Compiler 2.0 LLVM LLVM 2.0 C Objective-C C++ LLVM GCC 2 Xcode 4 LLVM IDE LLVM Xcode IDE C C++ Objective-C block lambda
  • 37.
    ■Blocks void(^b)() = ^{ printf("im in blockn"); }; b();
  • 38.
    ■Blocks void (^b)() = ^{ printf("im in blockn"); }; b(); C Block Block_copy(),Block_release()
  • 39.
    iOS4 Blocks ex) GCD( )
  • 40.
    Objective-C
  • 41.
    mixi graph API mixi graph API iPhone ※