iOS / Objective-C Meta Class and Category

Viewed 1406

I understand a class is actually an object / typedef of struct (objc_class*). Each class has a "backing" meta class, which in turns has a isa pointer pointing to the meta class of NSObject.

And NSObjectbasically has a isa pointer pointing back to itself.

And we could get a hold of the meta class via this line:

objc_getMetaClass("<CLASS_NAME>");

I hope my understanding of meta class is not off here so far (please correct me if it is).

My questions are:

1.)

When would we need to deal with meta class? Could you please cite an example / a scenario in code when we might need to?

2.)

Background:

I was thinking freely about third party libraries and how they are structured.

Usually they provide you with a class method and return you a class and all other methods are private / "hidden". And most of the time, while they can be subclassed but since we do not know the init method, it would be of no use to subclass.

And suddenly I began thinking about Objective-C Category and thus leading me to think of Objective-C meta class.

And this leads to my question #2:

Is it possible to break this structure by making use of Objective-C Category and / or with the use of meta class (by grabbing a hold of it and insert a class method straight there in the meta class)? Or even look up the name of their instance methods that are private?

My intention here is not to "break" anything, I am just very curious as to how "unbreakable" these third party libraries are structured and if their structures cannot be "broken" via the use of Category and Meta Class.

@Leo Natan:

1.)

What is method_*()?

2.)

For the love of Objective-C Runtime:

Could you cite an example implementation-swizzling an instance method, let say,

(I am not sure if this method is a good example, for we could override it in the first place)

NSArray's -count instance method (let’s make it so that it returns always count 100, for example)

3.)

So in theory all classes (including all third party libraries) can be break (broken)? Other words, there is no way to create a „call-only-this-class-method-or-you-cannot-use-me“ API / library?

Thanks a lot.

1 Answers
Related