首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

IOS4 note 11 (六)

2012-07-04 
IOS4 note 11 (6)The Secret Life of NSObject1.It defines some native class methods and instance meth

IOS4 note 11 (6)

The Secret Life of NSObject

1.It defines some native class methods and instance methods having mostly to do?with ?the basics of ?instantiation and of method sending and resolution. ?

2.It adopts the NSObject protocol. This protocol declares instance methods having?mostly to do with memory management, the relationship between an instance and?its class, and ?introspection. Because all ?the NSObject protocol methods are ?required, the NSObject class implements them all. (See the NSObject Protocol Reference.) This architecture is what permits NSProxy to be a root class; it, too, adopts?the NSObject protocol.

3.It implements convenience methods related to the NSCopying, NSMutableCopying, and NSCoding protocols, without formally adopting those protocols. NSObject intentionally doesn’t adopt these protocols because this would cause all other?classes to adopt them, which would be wrong. But thanks to this architecture, if a?class does adopt one of these protocols, you can call the corresponding convenience

method. For example, NSObject implements the copy instance method, so you can?call copy on any ?instance, but you’ll crash unless the ?instance’s class adopts the?NSCopying protocol and implements copyWithZone:.

4.A large number of methods are injected into NSObject by more than two dozen?informal protocols, which are actually categories on NSObject. For example, awakeFromNib comes ?from ?the ?UINibLoadingAdditions ?category ?on?NSObject, declared in UINibLoading.h.

5.A class object, as explained in Chapter 4, is an object. Therefore all classes, which?are objects of type Class, inherit from NSObject. Therefore, any method defined as?an instance method by NSObject can be called on a class object as a class method!

For example, respondsToSelector: is defined as an instance method by NSObject,?but it can be treated as a class method and sent to a class object.

?

Creation, destruction, and memory management

Methods for creating an instance, such as alloc and copy, along with methods thatyou might override in order to learn when something is happening in the lifetime?of an object, such as initialize and dealloc,?plus methods that manage memory.

Class relationships

Methods for learning an object’s class and inheritance, such as class, superclass,?isKindOfClass:, and isMemberOfClass:.

To check the class of an instance (or class), use methods such as isKindOfClass:.?Direct ?comparison ?of ?two ?class ?objects, ?as ?in ?[someObject class] == [otherObject class], is rarely advisable, especially because a Cocoa instance’s class might?be ?a private, undocumented ?subclass of ?the ?class you ?expect. ?I mentioned ?this?already in connection with class clusters, and it can happen in other cases.

Object introspection and comparison

Methods for asking what would happen if an object were sent a certain message,?such as respondsToSelector:; for representing an object as a string (description,?used in debugging;); and for comparing objects (isEqual:).

Message response

Methods ?for meddling with what does happen when an object ?is sent a certain?message, such as doesNotRecognizeSelector:.?

Message sending

Methods for sending a message indirectly. For example, performSelector: takes a?selector as parameter, and sending it to an object tells that object to perform that?selector. This might seem identical to just sending that message to that object, but?what if you don’t know what message to send until runtime? Moreover, variants?on performSelector: allow you send a message on a specified ?thread, or send a?message after a certain amount of time has passed (performSelector:withObject:

afterDelay: and similar); this is called delayed performance.

Delayed performance is a valuable technique. You often need to let Cocoa finish?doing something, such as laying out interface, before proceeding to a further step;?delayed performance with a very ?short delay ?(even as ?short as ?zero ?seconds) ?is?enough to postpone a method call until after Cocoa has finished whatever it’s in?the middle of doing. Technically, it allows the current run loop to finish, completing and unwinding the entire current method call stack, before sending the specified selector. It can also be used for simple timing, such as when you want to do?something different depending whether the user taps twice in quick succession or?only once; basically, when the user first taps, you respond using delayed performance, to give the user time to tap again if two taps are intended.

热点排行