Objective C 基本介紹

710 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
710
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Objective C 基本介紹

    1. 1. Chap 4Objective-C Classes
    2. 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. 3. .h & .m files @interface MyClass : NSObjectabc.h -(NSString*) sayGoodnightJames; @end @implementation MyClass { // instance variable declarations go here (starting in iOS5) }abc.m -(NSString*) sayGoodnightJames { return @”Good night, James!”; } @end
    4. 4. Cocoa’s Own Header Files
    5. 5. Cocoa’s Own Header Files• You can’t see the source code for Cocoa.
    6. 6. Cocoa’s Own Header Files• You can’t see the source code for Cocoa.• Rely purely on the documentation (and experimentation).
    7. 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.
    8. 8. Class Methods
    9. 9. Class Methods• Factory method
    10. 10. Class Methods• Factory method • UIFont has a class method fontWithName:size:
    11. 11. Class Methods• Factory method • UIFont has a class method fontWithName:size: • Supply a name and a size, and return a UIFont object.
    12. 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. 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. 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.
    15. 15. Chap 5Objective-C Instances
    16. 16. How instance are created
    17. 17. How instance are created• Ready-Made Instances
    18. 18. How instance are created• Ready-Made Instances • NSString* s2 = [s uppercaseString];
    19. 19. How instance are created• Ready-Made Instances • NSString* s2 = [s uppercaseString];• Instatiation from scratch
    20. 20. How instance are created• Ready-Made Instances • NSString* s2 = [s uppercaseString];• Instatiation from scratch • SomeClass* aVariable = [[SomeClass alloc] init];
    21. 21. How instance are created• Ready-Made Instances • NSString* s2 = [s uppercaseString];• Instatiation from scratch • SomeClass* aVariable = [[SomeClass alloc] init];• Nib-Based Instantiation (見下頁)
    22. 22. Intialization (P. 79)NSArray* pep =[NSArray arrayWithObjects:@”cho”, @”james”,@”calvin”,@”crux”, nil];NSArray* pep =[[NSArray alloc] initWithObjects:@”cho”, @”james”,@”calvin”,@”crux”, nil];
    23. 23. Nib-Based InstatiationWhen 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. 24. PolymorphismUIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];UIView* v = b;[v setTitle:@”James!” forState:UIControlStateNormal];//compiler will complain!
    25. 25. Polymorphism (cont.)UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];UIView* v = b;[(UIButton*)v setTitle:@”James!” forState:UIControlStateNormal];//compiler is happy!
    26. 26. Keyword: self@implementation MyClass-(NSString*) greeting { return @”Good night, James!”;}-(NSString*) sayGoodNightJames { return [self greeting];}@end
    27. 27. Keyword: self (cont.)@implemtation Dog-(NSString*) bark { return @”Hi, James”;}-(NSString*) speak { return [self bark];}@end(P. 85 Figure 5-2)
    28. 28. Keyword: self (conc.)@implemtation Basenji : Dog-(NSString*) bark { return @””; //Empty string, Basenjis can’t bark.}@endBasenji* b = [[Basenji alloc] init];(P. 85 Figure 5-2)
    29. 29. Keyword: super@implemtation NoisyDog : Dog-(NSString*) bark { return [NSString stringWithFormat: @”%@ %@”, [super bark], [superbark]];}@end(P. 88 a UIView Controller example)
    30. 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. 31. Instance Variables and AccessorsDog* fido = [[Dog alloc] init];[fido setNumber: 42];int n = [fido number];//n = 42!
    32. 32. Properties@property(nonatomic) UIViewAutoresizing autoresizingMask@property(nonatomic) int numberfido.number = 43;int n = fido.number;

    ×