Overview
NSObject
is the root class of most Objective-C class hierarchies. Through NSObject
, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.
Class Methods
alloc
Returns a new instance of the receiving class.
Return Value
A new instance of the receiver.
Discussion
The isa
instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0
.
You must use an init...
method to complete the initialization process. For example:
TheClass *newObject = [[TheClass alloc] init]; |
Do not override alloc
to include initialization code. Instead, implement class-specific versions of init...
methods.
For historical reasons, alloc
invokes allocWithZone:
.
allocWithZone:
Returns a new instance of the receiving class.
Parameters
-
zone
-
This parameter is ignored.
Return Value
A new instance of the receiver.
Discussion
The isa
instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0
.
You must use an init...
method to complete the initialization process. For example:
TheClass *newObject = [[TheClass allocWithZone:nil] init]; |
Do not override allocWithZone:
to include any initialization code. Instead, class-specific versions of init...
methods.
This method exists for historical reasons; memory zones are no longer used by Objective-C.
performSelector:withObject:afterDelay:
Invokes a method of the receiver on the current thread using the default mode after a delay.
Parameters
-
aSelector
-
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
anArgument
-
The argument to pass to the method when it is invoked. Pass
nil
if the method does not take an argument.
delay
-
The minimum time before which the message is sent. Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.
Discussion
This method sets up a timer to perform the aSelector message on the current thread’s run loop. The timer is configured to run in the default mode (NSDefaultRunLoopMode
). When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector. It succeeds if the run loop is running and in the default mode; otherwise, the timer waits until the run loop is in the default mode.
If you want the message to be dequeued when the run loop is in a mode other than the default mode, use the performSelector:withObject:afterDelay:inModes:
method instead. If you are not sure whether the current thread is the main thread, you can use the performSelectorOnMainThread:withObject:waitUntilDone:
or performSelectorOnMainThread:withObject:waitUntilDone:modes:
method to guarantee that your selector executes on the main thread. To cancel a queued message, use the cancelPreviousPerformRequestsWithTarget: or cancelPreviousPerformRequestsWithTarget:selector:object: method.