The document discusses Objective-C classes and instances. It explains that classes have interfaces defined in .h files and implementations in .m files. Cocoa classes can only be interacted with through header files, not source code. The document also covers how instances are created, either from factory methods or by alloc-init, as well as nib-based instantiation. Keywords like self and super, instance variables, accessors, and properties are also summarized.
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.
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.
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!
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!