10. • .m ( )
#import “Car.h”
@implementation Car
-(id) init The init method defined in the NSObject class
does no initialization, it simply returns self
{
self = [super init];
if(!self) = return nil;
make = nil;
model = nil;
year = 1901;
return self;
}
23. Scope of Instance Variables
Directive
Meaning
@private
The instance variable is accessible only within the class that declares it.
@protected
The instance variable is accessible within the class that declares it and within .
classes that inherit it.
All instance variables without an explicit scope directive have @protected @interface Worker : NSObject
scope.
{
@public
char *name;
The instance variable is accessible everywhere.
@private
@package int age;
Using the modern runtime, an @package instance variable has @public scope
inside the executable image that implements the class, but acts like @private char *evaluation;
outside.
@protected
The @package scope for Objective-C instance variables is analogous to
private_extern for C variables and functions. id job;
Any code outside the class implementation’s image that tries to use the
instance variable gets a link error. float wage;
This scope is most useful for instance variables in framework classes, where
@public
@private may be too restrictive but @protected or @public too permissive.
id boss;
}
protected: name, job, wage
private: age, evaluation
public: boss
24. •
•
• @property(attributes) type name;
• attributes : assign, retain, copy
• retain, copy : release message sent to the previous
value
• attributes: readonly, readwrite( readwrite)
• attribute : nonatomic (default is atomic)
32. Retain Count
When you create an object, it has a retain
count of 1.
• When you send an object a retain
message, its retain count is incremented by
1.
• When you send an object a release
message, its retain count is decremented by
1.
• When you send an object a autorelease
message, its retain count is decremented by
1 at some stage in the future.
33. Autorelease
• release
release
• release pool autorelease
release pool
release
•
34. •
•
• .
•
• To declare methods that others are expected to implement
• To declare the interface to an object while concealing its class
• To capture similarities among classes that are not hierarchically related
48. NSInvocation
An NSInvocation is an Objective-C message rendered static, that is, it is an action
turned into an object. NSInvocation objects are used to store and forward messages
between objects and between applications, primarily by NSTimer objects and the
distributed objects system.
An NSInvocation object contains all the elements of an Objective-C message: a target, a
selector, arguments, and the return value. Each of these elements can be set directly, and
the return value is set automatically when the NSInvocation object is dispatched.
An NSInvocation object can be repeatedly dispatched to different targets; its arguments
can be modified between dispatch for varying results; even its selector can be changed to
another with the same method signature (argument and return types). This flexibility
makes NSInvocation useful for repeating messages with many arguments and
variations; rather than retyping a slightly different expression for each message, you
modify the NSInvocation object as needed each time before dispatching it to a new
target.
NSInvocation does not support invocations of methods with either variable numbers of
arguments or union arguments. You should use the invocationWithMethodSignature:
class method to create NSInvocation objects; you should not create these objects using
alloc and init.
This class does not retain the arguments for the contained invocation by default. If those
objects might disappear between the time you create your instance of NSInvocation and
the time you use it, you should explicitly retain the objects yourself or invoke the
retainArguments method to have the invocation object retain them itself.
Note: NSInvocation conforms to the NSCoding protocol, but only supports coding by an
NSPortCoder. NSInvocation does not support archiving.