Xiaogu bottom blog collection

  • Today solve 2 to classic interview questions using the ISA Move chart (these two interview questions can show if you have researched the ground floor, know the ISA move, and have a fucking hole!).

  • Let’s start with the super classic ISA trend:

Interview Question 1

If the interview question is so long, people will panic! Just take your time! There is no time limit!

Objc4-787.1 Compiling and debugging

  • The code:
@interface XGPerson : NSObject
- (void)sayHello;
+ (void)sayHappy;
@end

@implementation XGPerson
- (void)sayHello{
    NSLog(@"XGPerson say : Hello!!!");
}
+ (void)sayHappy{
    NSLog(@"XGPerson say : Happy!!!");
}
@end

void xgClassMethod_classToMetaclass(Class pClass){
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);
    
    Method method1 = class_getClassMethod(pClass, @selector(sayHello));
    Method method2 = class_getClassMethod(metaClass, @selector(sayHello));
    Method method3 = class_getClassMethod(pClass, @selector(sayHappy)); 
    Method method4 = class_getClassMethod(metaClass, @selector(sayHappy));
    // 
    NSLog(@"%s-%p-%p-%p-%p",__func__,method1,method2,method3,method4);
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        XGPerson *person = [XGPerson alloc];
        Class pClass     = object_getClass(person);
        xgClassMethod_classToMetaclass(pClass);
    }
    return 0;
}
Copy the code

Q: 0 means no result, 1 means there is a result, what is the output?

  • Explanation:
  • Let’s start analyzing the problem!
  • 1. The main methods are:xgClassMethod_classToMetaclass
  • 2, the end result is to viewmethodIf there is a
      1. We started to explorexgClassMethod_classToMetaclass
      1. The first two lines of code get the metaclassmetaClass
      1. We can look at it firstclass_getClassMethodSource:
    Method class_getClassMethod(Class cls, SEL sel)
    Copy the code

{ if (! cls || ! sel) return nil;

return class_getInstanceMethod(cls->getMeta(), sel);
Copy the code

} // This is essentially to check whether his metaclass has instance methods!

* 4. Then we can see how 'CLS' 'getMeta()` ```Objc Class getMeta() {if (isMetaClass()) return (Class)this; else return this->ISA(); }Copy the code
    1. We have all the information we need. Then we can find out!!
    1. A. the b. the C. the D. the

  1. method1Is to look atpClassIs there asayHello.sayHelloIs an instance method, exists in a class! Then know from the source code:class_getInstanceMethod(cls->getMeta(), sel);He looks in his metaclasssayHelloSo I can’t find it! So it is0

  1. method2Is to look atmetaClassIs there asayHello.sayHelloIs an instance method that goes into the metaclass of the metaclass (getMeta, it isThe metaclassWords return to oneself) look forsayHelloSo I can’t find it! So it is0

  1. method3First of allsayHappyClass method!method3In thepClassWhether there are instance methods in the metaclass ofsayHappy, so it is1

  1. method4It’s kind of interesting! .sayHappyClass method! This should be in theThe metaclass of a metaclassGet inside! , is about to seeThe isa trend chart! And,getMetaIf it is a metaclass, return itself! The general meaning is: fromThe metaclassIn looking forInstance methods:sayHappySo the answer is1
    1. View the output:

    1. The answer:xgClassMethod_classToMetaclass-0-0-1-1

Class stores instance methods, and metaclass stores class methods (in the form of instance methods)

Interview question 2

  • (Q: What is the output?)
		BOOL re1 = [(id) [NSObject class] isKindOfClass:[NSObject class]].BOOL re2 = [(id) [NSObject class] isMemberOfClass:[NSObject class]].BOOL re3 = [(id)[XGPerson class] isKindOfClass:[XGPerson class]].BOOL re4 = [(id)[XGPerson class] isMemberOfClass:[XGPerson class]].NSLog(@" re1 :%hhd\n re2 :%hhd\n re3 :%hhd\n re4 :%hhd\n",re1,re2,re3,re4);

        BOOL re5 = [(id) [NSObject alloc] isKindOfClass:[NSObject class]].BOOL re6 = [(id) [NSObject alloc] isMemberOfClass:[NSObject class]].BOOL re7 = [(id)[XGPerson alloc] isKindOfClass:[XGPerson class]].BOOL re8 = [(id)[XGPerson alloc] isMemberOfClass:[XGPerson class]].NSLog(@" re5 :%hhd\n re6 :%hhd\n re7 :%hhd\n re8 :%hhd\n",re5,re6,re7,re8);
Copy the code
  • Come, old rules, let’s need the source code all stick out!

// isKindOfClass is handled by LLVM during compilation: objc_opt_isKindOfClass

// Calls [obj isKindOfClass]
BOOL
objc_opt_isKindOfClass(id obj, Class otherClass)
{
#if __OBJC2__
    if(slowpath(! obj))return NO;
    Class cls = obj->getIsa();
    if(fastpath(! cls->hasCustomCore())) {for (Class tcls = cls; tcls; tcls = tcls->superclass) {
            if (tcls == otherClass) return YES;
        }
        return NO;
    }
#endif
    return ((BOOL(*) (id, SEL, Class))objc_msgSend)(obj, @selector(isKindOfClass:), otherClass);
}


/ / class methods
+ (BOOL)isMemberOfClass:(Class)cls {
    return self->ISA() == cls;
}

// Instance method
- (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
}
Copy the code
  • All right! Analysis again! You’d better think twice before you read my answer!

    1. Both the class method and the instance method isKindOfClass go to objc_opt_isKindOfClass.
    • Class1 isKindOfClass: class2: obj is the parent of the class to which obj’s isa points, equivalent to class2, returns YES when it isa class method.

    • 1.2 is the instance methods: object1 isKindOfClass class: obj object1, according to the flow chart of the isa: obj isa point to create his class, so it means: if object1 is a class of objects created by the class or subclass.

So the class method can tell the values of re1 and re3:

Re1: NSObject – (isa) — – > NSObject (yuan) – (inheritance) – > NSObject

NSObject == NSObject! So it is 1

Re3: XGPerson – (isa) — – > XGPerson (yuan) – (inheritance) – > NSObject

XGPerson == NSObject! So it’s 0.

So the instance method knows re5 and RE7

Re5: [NSObject alloc] is the object created by NSObject

Re7: [XGPerson alloc] is the object created by XGPerson

So it’s all 1’s

Extension: BOOL re7 = [(id)[XGPerson Alloc] isKindOfClass:[NSObject class]]; In that case, re7 is also 1


    1. Analysis class methods:A isMemberOfClass BIs:AtheisaWhether or notBThe same

** classes and metaclasses may have the same name, but they are not the same thing. 台湾国

So we can know the values of RE2 and re4:

Re2: NSObject–(ISA)–>NSObject(metaclass)

Re4: XGPerson–(isa)–>XGPerson(metaclass)

So they’re both 0’s


    1. Instance methodsobject2 isMemberOfClass class2: [object2 class]Whether or notclass2The same

So we know re6 and re8

re6:[NSObject alloc].class == NSObject

re8:[XGPerson alloc].class == XGPerson

So it’s all 1’s

Guys!! These two interview questions are a bit long!! , hope to help you ~~, I am so step by step to explain to understand!