iOS NSObject Class 详解

来源:互联网 发布:苹果cms 视频云解析 编辑:程序博客网 时间:2024/04/24 13:22

Overview

NSObject is the root class of most Objective-C class hierarchies. Through NSObjectobjects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.

NSObject是大部分Object-C类层次的根类。通过这个类,对象继承运行时系统的基本接口,有了Objective-C对象的行为。

Class Methods

alloc

Returns a new instance of the receiving class.

+ (id)alloc
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:.

这个方法,返回类的一个新的实例。
新实例的isa实例变脸被初始化到定义在类中的一个数据结构中。其他的实例变量是0。你必须使用 init 方法去完成初始化过程。
不要复写alloc方法去添加初始化的代码,相反你得实现init 方法。由于历史原因,alloc调用allocWithZone:方法。

allocWithZone:

Returns a new instance of the receiving class.

+ (id)allocWithZone:(NSZone *)zone
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.

和alloc方法差不多。要注意的是,Objective-C不再使用memory zones。

performSelector:withObject:afterDelay:

Invokes a method of the receiver on the current thread using the default mode after a delay.

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
Parameters
aSelector

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.


这个方法在当前线程的run loop里设定一个计时器执行aSelector消息。

[未完,待续]
0 0