Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are classes objects in Objective-C?

Tags:

okay, so I understand that an object is an instance of a class that must be allocated and initialized, but are classes themselves objects?

I know when you create a new class it is an instance of something else, like NSObject. So, if this makes it a class, then objects can hold not only variables and methods, but other objects as well, right?

Sorry, this is probably really basic, but I am reading two books about cocoa and xcode and this point is a little unclear (probably because of my lack of experience in other languages).

like image 934
jerry Avatar asked Apr 24 '11 20:04

jerry


People also ask

Is there classes and objects in C?

The main purpose of Objective-C programming language is to add object orientation to the C programming language and classes are the central feature of Objective-C that support object-oriented programming and are often called user-defined types.

Are classes an instance of an object?

An instance of a class is an object. It is also known as a class object or class instance. As such, instantiation may be referred to as construction. Whenever values vary from one object to another, they are called instance variables.

How do I find the class of an object in Objective-C?

[yourObject isKindOfClass:[a class]] // Returns a Boolean value that indicates whether the receiver is an instance of // given class or an instance of any class that inherits from that class.

How do you create an instance of a class in Objective-C?

You can create a class reference with the following code: Class M = [NSMutableString class]; // NSMutableString (for example). You can then call methods on that saved class with code like this: [M string];


1 Answers

Here is a pretty good explanation of the matter by Greg Parker

Quoting:

[...] Each Objective-C class is also an object. It has an isa pointer and other data, and can respond to selectors. When you call a "class method" like [NSObject alloc], you are actually sending a message to that class object.

Since a class is an object, it must be an instance of some other class: a metaclass. The metaclass is the description of the class object, just like the class is the description of ordinary instances. In particular, the metaclass's method list is the class methods: the selectors that the class object responds to. When you send a message to a class - an instance of a metaclass - objc_msgSend() looks through the method list of the metaclass (and its superclasses, if any) to decide what method to call. Class methods are described by the metaclass on behalf of the class object, just like instance methods are described by the class on behalf of the instance objects.

What about the metaclass? Is it metaclasses all the way down? No. A metaclass is an instance of the root class's metaclass; the root metaclass is itself an instance of the root metaclass. The isa chain ends in a cycle here: instance to class to metaclass to root metaclass to itself. The behavior of metaclass isa pointers rarely matters, since in the real world nobody sends messages to metaclass objects. [...]

Further interesting reads:

Understanding the Objective-C Runtime by Colin Wheeler
(search for paragraph titled "So Classes define objects…")

What is a meta-class in Objective-C? by Matt Gallagher

like image 108
Regexident Avatar answered Oct 22 '22 14:10

Regexident