iOS Details NSObject Protocol

Keywords: iOS

_protocol is a set of interfaces that are declared to have all the methods in the protocol after a protocol has been complied with. The NSObject class complies with the NSObject protocol and implements all the methods in the NSObject protocol, so the NSObject class and its subclasses can call these methods. This paper mainly introduces the methods in NSObject protocol.
(1)

- (BOOL)isEqual:(id)object;

"==" determines whether or not the same object is compared with the memory address. "isEqual" determines whether two pairs of objects are identical and compares whether the values of member variables are identical.

@interface Person : NSObject

@property (nonatomic, strong) NSString *father;
@property (nonatomic, assign) NSUInteger age;

@end

@implementation Person

@end
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  
    [super viewDidLoad];
    
    Person *xiaoming = [Person new];
    xiaoming.father = @"laowang";
    xiaoming.age = 12;
    
    Person *xiaohong = [Person new];
    xiaohong.father = @"laowang";
    xiaohong.age = 12;
    
    NSLog(@"xiaoming == xiaohong = %d",xiaoming == xiaohong);
    NSLog(@"xiaohong isEqual:xiaohong = %d",[xiaohong isEqual:xiaohong]);
}
2017-05-06 09:26:51.799 OCTest[2401:832010] xiaoming == xiaohong = 0
2017-05-06 09:26:51.799 OCTest[2401:832010] xiaohong isEqual:xiaohong = 1

(2)

@property (readonly) NSUInteger hash;

_Hash Table's key, used for set and dictionary, defaults to the address of the object in memory, guaranteeing uniqueness.

- (void)viewDidLoad {
  
    [super viewDidLoad];
    
    NSLog(@"self = %ld",self);
    NSLog(@"self.hash = %u",self.hash);
}
2017-05-06 09:32:01.116 OCTest[2408:832939] self = 384375648
2017-05-06 09:32:01.117 OCTest[2408:832939] self.hash = 384375648

(3)

@property (readonly) Class superclass;

The superclass of the NSObject instance is NULL.
(4)

- (Class)class;

Obtain the class of the object. '+ (Class)class;'obtains the metaclass of the class object;
(5)

- (instancetype)self;

Obtain the object itself.
(6)

- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

_is an object calling a method, object is a parameter, at most two.
(7)

- (BOOL)isProxy;

_is it an instance of NSProxy? NSObject is the base class of most classes, with some exceptions being instances of NSProxy. NSProxy is a very magical class that can be used to mimic multiple inheritance because it can forward messages and specify an instance to execute messages.
(8)

- (BOOL)isKindOfClass:(Class)aClass;

_Judge whether an object is an instance of a class or a subclass.
(9)

- (BOOL)isMemberOfClass:(Class)aClass;

Judge whether an object is an instance of a class.
(10)

- (BOOL)conformsToProtocol:(Protocol *)aProtocol;

_It is not necessary to judge whether an instance complies with the protocol or whether the method in the protocol has been implemented. This method can only judge whether the method in the protocol is declared or not, and whether the implementation is judged by "respondsToSelector".
(11)

- (BOOL)respondsToSelector:(SEL)aSelector;

_Whether it responds to a method or not. Before the proxy passes the value, it must decide whether it responds to the method in the protocol, otherwise it will collapse.
_Except for the NSObject protocol, all other protocols must comply with the NSObject protocol. Why is that? What happens if you don't comply? I can tell you clearly that it will collapse and report an error of "unrecognized selector sent to instance 0x15d78700". Because the proxy calls the method in the NSObject protocol. The proxy declaration is of type id, and uncertainty is an instance of NSObject, so the method in the NSObject protocol cannot be invoked.

@protocol PersonDelegate <NSObject>
@end

(12)

- (instancetype)retain;
- (oneway void)release;
- (instancetype)autorelease;
- (NSUInteger)retainCount;
- (struct _NSZone *)zone ;

_It's all ARC now. It's really outdated to call the method that MRC can use.
(13)

@property (readonly, copy) NSString *description;
@optional
@property (readonly, copy) NSString *debugDescription;

_description is a method that is called when the code prints out the instance. debug description is a method that is called when the console or po outputs the instance. It can be customized. In fact, the methods in NSObject protocol can be customized according to their own needs.

Posted by Flyier on Tue, 02 Jul 2019 16:43:59 -0700