Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Understanding Class Objects and Selectors in Objective-C

Notes 3

The Nature of Class Objects

In Objectvie-C, a class is itself an object, known as a class object.

A class object persists throughout the runtime of the program. It is a data structure that stores essential information about the class, such as its size, name, version, and the dispatch table mapping selectors to method implementations. This information is determined at compile time and loaded into memory when the class is first used.

The class object represents the class. The Class type denotes a class object, and class methods belong to it. When a message receiver is a class name, that name refers to the class object.

During runtime, all instances of a class are created by its class object. The class object sets the instance's isa pointer too its own address, meaning every instance's isa points to its class object. From the class object, you can discover superclass information and the methods it can respond to.

Class objects can only use class methods, not instance methods.

Example:

Employee *emp = [Employee new]; // 'emp' is an instance object
// 'Employee' is also an object (a class object) of type Class

Obtaining a Class Object

You can retrieve a class object in two primary ways:

1. From an instance object:

Vehicle *car = [Vehicle new];
Vehicle *truck = [Vehicle new];

// Get the class object from instances
Class carClass = [car class];
Class truckClass = [truck class];

NSLog(@"%p", carClass);
NSLog(@"%p", truckClass); // Both will print the same address

2. Directly from the class name:

// The class name itself represents the class object
Class vehicleClass = [Vehicle class];
NSLog(@"%p", vehicleClass); // Same address as above

Using a Class Object

Traditionally, you use a class name to:

  • Create instances: [Person new];
  • Call class methods: [Person utilityMethod];

With a Class variable, you can perform similar operations:

Person *personInstance = [Person new];
Class personClass = [personInstance class]; // Get the Class object

// 1. Create a new instance using the Class object
Person *anotherPerson = [personClass new];

// 2. Call a class method using the Class object
// Asuming +staticMethod is a class method
[personClass staticMethod];

Note: [personInstance instanceMethod] calls an instance method (dynamic), while [personClass staticMethod] calls a class method (static).

The SEL Type

SEL (selector) is a type that represents the location of a method.

Method Lookup Process:

  1. The method name (e.g., display) is wrapped into a SEL type data.
  2. The runtime uses this SEL to find the corresponding method address in the class's dispatch table.
  3. The method at that address is invoked.
  4. This process employs caching. The first lookup can be expensive, but subsequent calls for the same selector are fast.

Every method implementation has access to the _cmd variable, which represents the current method's selector.

SEL is essentially a wrapper for a method name, enabling the runtime to find and call the method. Sending a message involves sending a SEL.

Example of using SEL:

// Manually create a SEL for the 'calculate' method
SEL actionSelector = @selector(calculate);

// Use performSelector: to invoke the method via the SEL
[myObject performSelector:actionSelector];

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.