SlideShare a Scribd company logo
Chap 4
Objective-C Classes
Interface and
            implementation
@interface MyClass : NSObject
-(NSString*) sayGoodnightJames;
@end
@implementation MyClass {
    // instance variable declarations go here (starting in iOS5)
}
-(NSString*) sayGoodnightJames {
    return @”Good night, James!”;
}
@end
.h & .m files
        @interface MyClass : NSObject
abc.h   -(NSString*) sayGoodnightJames;
        @end
        @implementation MyClass {
            // instance variable declarations go here (starting in iOS5)
        }
abc.m   -(NSString*) sayGoodnightJames {
            return @”Good night, James!”;
        }
        @end
Cocoa’s Own Header
        Files
Cocoa’s Own Header
        Files

• You can’t see the source code for Cocoa.
Cocoa’s Own Header
        Files

• You can’t see the source code for Cocoa.
• Rely purely on the documentation (and
  experimentation).
Cocoa’s Own Header
        Files

• You can’t see the source code for Cocoa.
• Rely purely on the documentation (and
  experimentation).
• Can only see the Cocoa header files.
Class Methods
Class Methods
•   Factory method
Class Methods
•   Factory method

    •   UIFont has a class method fontWithName:size:
Class Methods
•   Factory method

    •   UIFont has a class method fontWithName:size:
    •   Supply a name and a size, and return a UIFont
        object.
Class Methods
•   Factory method

    •   UIFont has a class method fontWithName:size:
    •   Supply a name and a size, and return a UIFont
        object.
•   Global utility method
Class Methods
•   Factory method

    •   UIFont has a class method fontWithName:size:
    •   Supply a name and a size, and return a UIFont
        object.
•   Global utility method
    •   Good place to put a utility method.
Class Methods
•   Factory method

    •   UIFont has a class method fontWithName:size:
    •   Supply a name and a size, and return a UIFont
        object.
•   Global utility method
    •   Good place to put a utility method.
    •   Doesn’t require the overhead of an instance.
Chap 5
Objective-C Instances
How instance are
   created
How instance are
       created
• Ready-Made Instances
How instance are
       created
• Ready-Made Instances
 • NSString* s2 = [s uppercaseString];
How instance are
       created
• Ready-Made Instances
 • NSString* s2 = [s uppercaseString];
• Instatiation from scratch
How instance are
        created
• Ready-Made Instances
 • NSString* s2 = [s uppercaseString];
• Instatiation from scratch
 •   SomeClass* aVariable = [[SomeClass alloc] init];
How instance are
        created
• Ready-Made Instances
 • NSString* s2 = [s uppercaseString];
• Instatiation from scratch
 •   SomeClass* aVariable = [[SomeClass alloc] init];

• Nib-Based Instantiation (見下頁)
Intialization (P. 79)
NSArray* pep =

[NSArray arrayWithObjects:@”cho”, @”james”,
@”calvin”,@”crux”, nil];




NSArray* pep =

[[NSArray alloc] initWithObjects:@”cho”, @”james”,
@”calvin”,@”crux”, nil];
Nib-Based Instatiation
When the app runs and a nib file is loaded,
those classes are instantiated and initialized.
(P. 80 Figure 5-1)
UIButton* b =

  [UIButton buttonWithType:UIButtonTypeRoundedRect]; //factory method

[b setTitle:@”Hello!” forState:UIControlStateNormal]; //set up title

[b setFrame: CGRectMake(100,100,100,35)]; //set up frame

[view addSubview:b]; //place a button in view
Polymorphism
UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIView* v = b;

[v setTitle:@”James!” forState:UIControlStateNormal];

//compiler will complain!
Polymorphism (cont.)
UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIView* v = b;

[(UIButton*)v setTitle:@”James!” forState:UIControlStateNormal];

//compiler is happy!
Keyword: self
@implementation MyClass
-(NSString*) greeting {
    return @”Good night, James!”;
}
-(NSString*) sayGoodNightJames {
    return [self greeting];
}
@end
Keyword: self (cont.)
@implemtation Dog

-(NSString*) bark {

    return @”Hi, James”;

}

-(NSString*) speak {

    return [self bark];

}

@end

(P. 85 Figure 5-2)
Keyword: self (conc.)
@implemtation Basenji : Dog

-(NSString*) bark {

    return @””;      //Empty string, Basenjis can’t bark.

}

@end

Basenji* b = [[Basenji alloc] init];

(P. 85 Figure 5-2)
Keyword: super
@implemtation NoisyDog : Dog

-(NSString*) bark {

  return [NSString stringWithFormat: @”%@ %@”, [super bark], [super
bark]];

}

@end

(P. 88 a UIView Controller example)
Instance Variables and
      Accessors
@implementation Dog {
    // ivars can now be declared in the implementation section
    int number;
}
-(void) setNumber: (int) n {
     self->number = n;
}
-(int) number {
     return self->number;
}
@end
Instance Variables and
      Accessors
Dog* fido = [[Dog alloc] init];

[fido setNumber: 42];

int n = [fido number];

//n = 42!
Properties
@property(nonatomic) UIViewAutoresizing autoresizingMask

@property(nonatomic) int number

fido.number = 43;

int n = fido.number;

More Related Content

PDF
Django REST Framework
PPS
Underscore
KEY
PTW Rails Bootcamp
PDF
Djangocon 2014 angular + django
KEY
Underscore.js
PPTX
Introduction to Underscore.js
PPT
KEY
Rails with mongodb
Django REST Framework
Underscore
PTW Rails Bootcamp
Djangocon 2014 angular + django
Underscore.js
Introduction to Underscore.js
Rails with mongodb

What's hot (20)

PDF
RSpec 2 Best practices
PDF
Play á la Rails
PDF
Django rest framework tips and tricks
PDF
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
PDF
Automated testing with RSpec
KEY
ISUCONアプリを Pythonで書いてみた
KEY
Impression of Rails 3
PDF
2017 JCP EC: Configuration JSR
PDF
Voyage by example
PDF
Love / Hate Puppet (Puppet Gotchas)
PDF
WordPress hooks - WPLDN July 2013 Meetup
PDF
Introduction to Underscore.js
PDF
Go database/sql
PDF
ARCでめちゃモテiOSプログラマー
KEY
Djangocon
PPTX
Reasons To Love Ruby
PDF
Everybody Loves AFNetworking ... and So Can you!
PDF
Extracting ruby gem
PDF
Modernizes your objective C - Oliviero
KEY
2011/10/08_Playframework_GAE_to_Heroku
RSpec 2 Best practices
Play á la Rails
Django rest framework tips and tricks
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Automated testing with RSpec
ISUCONアプリを Pythonで書いてみた
Impression of Rails 3
2017 JCP EC: Configuration JSR
Voyage by example
Love / Hate Puppet (Puppet Gotchas)
WordPress hooks - WPLDN July 2013 Meetup
Introduction to Underscore.js
Go database/sql
ARCでめちゃモテiOSプログラマー
Djangocon
Reasons To Love Ruby
Everybody Loves AFNetworking ... and So Can you!
Extracting ruby gem
Modernizes your objective C - Oliviero
2011/10/08_Playframework_GAE_to_Heroku
Ad

Viewers also liked (10)

KEY
20120720品牌原來如此分享
PPTX
Jakobsena polacco pp
PPT
More Guns, Less Crime
DOC
Surabh airtel
PPT
2caldecottbooks
PPT
資金不是創業最難的事, 擁抱夢想創業
PPS
6 mathematics of_life1
PDF
Session 3 - Object oriented programming with Objective-C (part 1)
PPT
Il gladiatore, l'età imperiale attraverso il film di Ridley Scott
KEY
[KSG知識分享會]20120720品牌原來如此分享
20120720品牌原來如此分享
Jakobsena polacco pp
More Guns, Less Crime
Surabh airtel
2caldecottbooks
資金不是創業最難的事, 擁抱夢想創業
6 mathematics of_life1
Session 3 - Object oriented programming with Objective-C (part 1)
Il gladiatore, l'età imperiale attraverso il film di Ridley Scott
[KSG知識分享會]20120720品牌原來如此分享
Ad

Similar to Objective C 基本介紹 (20)

PDF
201005 accelerometer and core Location
PDF
iOS Programming Intro
PDF
Louis Loizides iOS Programming Introduction
PPT
Objective c
PPTX
iOS Session-2
KEY
Fwt ios 5
PPT
Objective-C for iOS Application Development
PPTX
Presentation 3rd
PDF
Iphone course 1
PDF
MFF UK - Introduction to iOS
PDF
Objective-C Is Not Java
PDF
FI MUNI 2012 - iOS Basics
KEY
Objective-C Crash Course for Web Developers
KEY
Frederick web meetup slides
PDF
03 Custom Classes
PDF
02 objective-c session 2
PDF
Crash Course in Objective-C
PPTX
Ios development
PDF
iOS 101 - Xcode, Objective-C, iOS APIs
PPT
Objective c intro (1)
201005 accelerometer and core Location
iOS Programming Intro
Louis Loizides iOS Programming Introduction
Objective c
iOS Session-2
Fwt ios 5
Objective-C for iOS Application Development
Presentation 3rd
Iphone course 1
MFF UK - Introduction to iOS
Objective-C Is Not Java
FI MUNI 2012 - iOS Basics
Objective-C Crash Course for Web Developers
Frederick web meetup slides
03 Custom Classes
02 objective-c session 2
Crash Course in Objective-C
Ios development
iOS 101 - Xcode, Objective-C, iOS APIs
Objective c intro (1)

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Cloud computing and distributed systems.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Weekly Chronicles - August'25 Week I
20250228 LYD VKU AI Blended-Learning.pptx
Cloud computing and distributed systems.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks

Objective C 基本介紹

  • 2. Interface and implementation @interface MyClass : NSObject -(NSString*) sayGoodnightJames; @end @implementation MyClass { // instance variable declarations go here (starting in iOS5) } -(NSString*) sayGoodnightJames { return @”Good night, James!”; } @end
  • 3. .h & .m files @interface MyClass : NSObject abc.h -(NSString*) sayGoodnightJames; @end @implementation MyClass { // instance variable declarations go here (starting in iOS5) } abc.m -(NSString*) sayGoodnightJames { return @”Good night, James!”; } @end
  • 5. Cocoa’s Own Header Files • You can’t see the source code for Cocoa.
  • 6. Cocoa’s Own Header Files • You can’t see the source code for Cocoa. • Rely purely on the documentation (and experimentation).
  • 7. Cocoa’s Own Header Files • You can’t see the source code for Cocoa. • Rely purely on the documentation (and experimentation). • Can only see the Cocoa header files.
  • 9. Class Methods • Factory method
  • 10. Class Methods • Factory method • UIFont has a class method fontWithName:size:
  • 11. Class Methods • Factory method • UIFont has a class method fontWithName:size: • Supply a name and a size, and return a UIFont object.
  • 12. Class Methods • Factory method • UIFont has a class method fontWithName:size: • Supply a name and a size, and return a UIFont object. • Global utility method
  • 13. Class Methods • Factory method • UIFont has a class method fontWithName:size: • Supply a name and a size, and return a UIFont object. • Global utility method • Good place to put a utility method.
  • 14. Class Methods • Factory method • UIFont has a class method fontWithName:size: • Supply a name and a size, and return a UIFont object. • Global utility method • Good place to put a utility method. • Doesn’t require the overhead of an instance.
  • 16. How instance are created
  • 17. How instance are created • Ready-Made Instances
  • 18. How instance are created • Ready-Made Instances • NSString* s2 = [s uppercaseString];
  • 19. How instance are created • Ready-Made Instances • NSString* s2 = [s uppercaseString]; • Instatiation from scratch
  • 20. How instance are created • Ready-Made Instances • NSString* s2 = [s uppercaseString]; • Instatiation from scratch • SomeClass* aVariable = [[SomeClass alloc] init];
  • 21. How instance are created • Ready-Made Instances • NSString* s2 = [s uppercaseString]; • Instatiation from scratch • SomeClass* aVariable = [[SomeClass alloc] init]; • Nib-Based Instantiation (見下頁)
  • 22. Intialization (P. 79) NSArray* pep = [NSArray arrayWithObjects:@”cho”, @”james”, @”calvin”,@”crux”, nil]; NSArray* pep = [[NSArray alloc] initWithObjects:@”cho”, @”james”, @”calvin”,@”crux”, nil];
  • 23. Nib-Based Instatiation When the app runs and a nib file is loaded, those classes are instantiated and initialized. (P. 80 Figure 5-1) UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //factory method [b setTitle:@”Hello!” forState:UIControlStateNormal]; //set up title [b setFrame: CGRectMake(100,100,100,35)]; //set up frame [view addSubview:b]; //place a button in view
  • 24. Polymorphism UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect]; UIView* v = b; [v setTitle:@”James!” forState:UIControlStateNormal]; //compiler will complain!
  • 25. Polymorphism (cont.) UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect]; UIView* v = b; [(UIButton*)v setTitle:@”James!” forState:UIControlStateNormal]; //compiler is happy!
  • 26. Keyword: self @implementation MyClass -(NSString*) greeting { return @”Good night, James!”; } -(NSString*) sayGoodNightJames { return [self greeting]; } @end
  • 27. Keyword: self (cont.) @implemtation Dog -(NSString*) bark { return @”Hi, James”; } -(NSString*) speak { return [self bark]; } @end (P. 85 Figure 5-2)
  • 28. Keyword: self (conc.) @implemtation Basenji : Dog -(NSString*) bark { return @””; //Empty string, Basenjis can’t bark. } @end Basenji* b = [[Basenji alloc] init]; (P. 85 Figure 5-2)
  • 29. Keyword: super @implemtation NoisyDog : Dog -(NSString*) bark { return [NSString stringWithFormat: @”%@ %@”, [super bark], [super bark]]; } @end (P. 88 a UIView Controller example)
  • 30. Instance Variables and Accessors @implementation Dog { // ivars can now be declared in the implementation section int number; } -(void) setNumber: (int) n { self->number = n; } -(int) number { return self->number; } @end
  • 31. Instance Variables and Accessors Dog* fido = [[Dog alloc] init]; [fido setNumber: 42]; int n = [fido number]; //n = 42!

Editor's Notes