IOS development diary 56-Analysis of NSobject API,

Source: Internet
Author: User

IOS development diary 56-Analysis of NSobject API,

Today, the blogger has a requirement to analyze NSobject API and has encountered some difficulties. I would like to share with you the hope to make common progress.

We all know that NSObject follows the NSObject protocol.

Protocol
  • -(BOOL) isEqual :( id) object

    • Check whether the object is the same, the address is the same, and the hash value must be the same before YES is returned.
  • @ Property (readonly) NSUInteger hash/+ (NSUInteger) hash

    • It is generally used for comparison. Note: newly created instances are generally different, and the static method return values of classes are the same. For example, a class named Man has the same [Man hash] value, man * man = [Man new]; [man hash] values are different
  • @ Property (readonly) Class superclass/+ (Class) superclass

    • Returns the class name of the parent class.
  • -(Class) class/+ (Class) class

    • Returns the Class Name of the current user.
  • -(Instancetype) self

    • Returns itself, equivalent to this in Java
  • -(Id) specify mselector :( SEL) aSelector withObject :( id) object1 withObject :( id) object2

    • Execute the aSelector method, with parameters. Compared with the [class do] method, the compilation will not report an error, but will only warn you to check whether this method is available during running. If not, crash
  • -(BOOL) isProxy

    • Check whether NSObject (such as NSProxy) is not inherited. If NO is returned
  • -(BOOL) isKindOfClass :( Class) aClass

    • Whether it is a member of the aClass class or aClass subclass
  • -(BOOL) isMemberOfClass :( Class) aClass

    • Whether it is a member of the aClass class
  • -(BOOL) conformsToProtocol :( Protocol) aProtocol
    + (BOOL) conformsToProtocol :( Protocol *) protocol

    • Compliance with the protocol
  • -(BOOL) respondsToSelector :( SEL) aSelector
    + (BOOL) instancesRespondToSelector :( SEL) aSelector

    • Whether the aSelector method is implemented
  • @ Property (readonly, copy) NSString description
    + (NSString *) description

    • Returns the instance class name and address, which can be rewritten.
  • @ Property (readonly, copy) NSString * debugDescription
    + (NSString) debugDescription

    • Use the po command during debugging. The description method is implemented by default. It is generally used to implement more specific class descriptions, such as memory addresses.
Methods
  • + (Void) load

    • The class loaded at runtime will be called (and only once) and can be used to operate the Swizzling Method. The call sequence is the parent class load-> own load-> category load, it is best not to call other classes in load, because it is likely that other classes have not been loaded.
  • + (Void) initialize

    • The first call to this class will be triggered (and only once), such as [class new] and [class alloc].
  • -(Instancetype) init

    • I don't want to talk about it anymore. It's a bad method.
  • + (Instancetype) new

    • It is equivalent to [class alloc] init. It can be new directly during simple initialization. It supports dot syntax.
  • + (Instancetype) allocWithZone :( struct _ NSZone *) zone

    • Methods left over due to historical reasons are generally not actively called. This method is triggered when alloc is used.
  • + (Instancetype) alloc

    • Returns an allocated memory object. It is best to use the two-segment method [class alloc] init. Otherwise, there may be unknown problems.
  • -(Void) dealloc

    • The MRC era is very important. It is used to release and clear some variables when the class is destroyed. In the ARC era, there is basically no need to rewrite them, except for the destruction of some timers or the removal of notifications.
  • -(Void) finalize

    • Some garbage collection tasks are generally not actively called.
  • -(Id) copy

    • CopyWithZone is required for immutable copying.
  • -(Id) mutableCopy

    • Variable copy
      @ Property (copy) NSMutableArrayArray or
      NSMutableArray
      Array = [array copy] is incorrect. After copying, the array becomes immutable, and subsequent operations will cause crash
  • -(IMP) methodForSelector :( SEL) aSelector
    + (IMP) instanceMethodForSelector :( SEL) aSelector

    • Obtain the aSelector method pointer. Usage: This method is frequently called and can be assembled into a method to improve performance.
  • -(Void) doesNotRecognizeSelector :( SEL) aSelector

    • Manually Create crash. Usage: the parent class provides pure virtual functions. For example, A has A method.
      - (void)show   {      // Subclasses need to override this method...       [self doesNotRecognizeSelector:_cmd];   }
      Therefore, to inherit the subclass of A and implement the show method, it must be rewritten. crash can be called directly.
  • -(BOOL) allowsWeakReference
    -(BOOL) retainWeakReference

    • OC has some classes and does not support ARC, such as NSMachPort class. This method can be used to determine whether ARC is supported.
  • + (BOOL) isSubclassOfClass :( Class) aClass

    • Class Method: determine whether it is a subclass of a class.

Message forwarding
Unrecognized selector sent to instance. This error should not be repeated. Calling a method that does not exist in the class causes Crash. We have three chances to save it before the Crash.

1. Self-Help (Dynamic addition method)
  • (BOOL) resolveInstanceMethod :( SEL) sel
2. Other types of forwarding
  • (Id) forwardingTargetForSelector :( SEL) aSelector
3. Sign and forward
    • (NSMethodSignature *) methodSignatureForSelector :( SEL) aSelector
    • (Void) forwardInvocation :( NSInvocation *) anInvocation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.