x
x
以下是对 Object-C 的摘要:
(1) 1979 C with Class, 1984 C++, 1984 objc,然后经过 24 年的演化至今。
(2) C++ Fast but Static
- static function dispatch
- compile time decision
- little runtime flexibility
(3) Object-C Dynamic to the Core
- dynamic message dispatch
- runtime decision making
- runtime class extensions
(4) A comparative example of calling a method
- C++: someObject->doSomething();
- Java: someObject.doSomething();
- Object-C: [someObject doSomething];
(5) Object-C Messaging
- [Employee newEmployee];
- [anEmployee setRaise:200];
- [anEmployee setWork:work withDeadline:today];
(6) Object-C Selector (很像 VBA 的 IDispatch Interface)
- [Employee newEmployee]; --> "newEmployee"
- [anEmployee setRaise:200]; --> "setRaise:"
(7) Method Dispatch,Object-C 只能继承单一 class,每一种 class 都是 NSObject 的 child class
(8) Method Dispatch -- What if no one response to the message?
- Runtime traverses class hierarchy again
- Each object is given an opportunity to forward the message
- Remote messaging works this way
- Throws exception if no one handles the message
(9) How a Object-C class looks like
@interface Person : NSObject {
NSString *name;
int age;
NSMutableArray *parent;
}
+ (Person*) personWithName: (NSString *)name; /* class only message */
- (id) initWithName: (NSString *) name;
@property(readwrite) int age;
@property(readwrite) (NSArray*) parents;
@end
(10) How to implement a class
#import "Person.h" /* case sensitive */
@implementation Person
@synthesize age, parents;
/* initializing your instance */
- (id) init {
if (self == [super init]) {
name = @"New Person";
parents = [[NSArray alloc] init];
}
return self;
}
/* cleaning up after ourselves */
- (void) dealloc {
[name release];
[parents release];
[super dealloc];
}
- (NSString *) name {
return name;
}
- (void) setName: (NSString*) newName {
if (newName != name) {
[newName retain]; /* same as COM interface addref() */
[name release]; /* same as COM interface release() */
name = newName;
}
}
/* custom behavior of property */
- (void) setParents: (NSArray*) newParents {
if (parents != newParents) {
[parents release];
parents = [newParents mutableCopy];
}
}
@end
(11) Reference Counting
MJFoo *myFoo = [[MJFoo alloc] init]; /* reference count = 1 */
[myFoo retain]; /* reference count = 2 */
[myFoo release]; /* reference count = 1 */
[myFoo release]; /* reference count = 0, and memory is reclaimed by system */
(12) Autorelease Pool -- a delayed release
- uses the -autorelease message
- sends -release to an object at some point in the future
- often used by class convenience methods
Example:
myString = [[NSString alloc] initWithString: @"foo"];
myString = [NSString stringWithString: @"foo"]; /* autorelease string */
(13) Setter/ Getter Pattern
- declare similar setters and getters each time
- implement similar access pattern
- more glue code usually means more bugs
(14) Properties -- Encapsulation Mad Easy
- Quickly and easily create setter/getter pairs
- Specify storage semantics
- Easy to override for custom behavior
Example:
@property(readwrite) float speed;
@property(copy) NSArray *passengers;
@property(nonatomic, copy) NSArray *passengers;
@property(getter=isGoingFast) BOOL goingFast
Property attributes:
readonly/readwrite
assign/retain/copy
nonatomic
setter= / getter=
(15) Object-C Categories -- Mixing your own behavior
- Seamlessly add methods to existing classes at runtime
- Subclass to add storage or modify existing behavior
@implementation NSString (myAdditions)
- (NSString *) rot13 {
....
}
@end
(16) Object-C Protocols -- Multiple inheritance via Specifications
- A list of method declarations
- Classes conform to a protocol by implement this list
- Identical to interface in Java
@protocol MyProtocol
- (void) foo: (int) x;
- (float) bar;
@end
Example:
#import "MyProtocol.h"
@interface Foo : NSObject
.....
@end