📜 ⬆️ ⬇️

Useful Facts about the Objective-C Programming Language

I have been developing iOS applications for 2 years now and in this article I wanted to present the facts that I found interesting and useful. I would be glad if you also share your knowledge in the comments. In the next article I would like to gather similar facts about the Foundation Kit.

.m


The .m (message) extension was introduced to highlight the key feature of Objective-C. In essence, we do not call the methods of the class, we send a message to the object, after which dispatching takes place during which the Objective-C method manager searches for the necessary class and calls the required method.
')
NS


The prefix NS stands for Next Step. It originated in the days when there was no Cocoa, and the framework was called NextSTEP and was a product of NeXT Software. Apple bought this company in 1996 and continued to use this prefix in order not to violate the backward compatibility code.


#include & #import


In C, use include guard to avoid recursive inclusion of header files.
In Objective-C, this function is built into #import.

Bool


Objective-C BOOL is actually a typedef for a signed char that uses 8 bits of memory. YES is defined as 1, and NO is defined as 0 using the #define directive.
Objective-C does not consider BOOL as a true logical type that is able to accept only two values ​​- YES and NO. The compiler treats BOOL as an 8-bit number, and the value YES and NO are merely conventions. This leads to subtle errors: if you are not careful enough and assign a value of more than one byte to a BOOL type variable, such as short or int, then only the lower byte will be used for the logical value. If this byte is accidentally zero (for example, in the case of 8960 (hexadecimal value 0x2300)), then the value BOOL will also be zero, that is, the value NO. For this reason, there is no point when comparing a BOOL value directly with YES.

- (void) myMethod


The leading dash indicates that this is an Objective-C method declaration. This dash is intended to distinguish a method declaration from a function prototype.

isa


The NSObject class declares one isa instance variable in order to store a pointer to an object class in it. The variable bears this name, because it actually defines the relation “is a” (is) between the subclass and the superclass, i.e. The rectangle is a shape, and the circle is a shape.

Base plus offset


When working, the compiler uses the “base plus offset” mechanism. Having the base address of the object - i.e. the location in the memory of the first byte of the first instance variable — the compiler can find all the other instance variables by adding certain offsets to this base address.

This problem has been solved in the new 64-bit Objective-C runtime system, where indirectness is used to determine the location of instance variables.

set & get


Everyone knows that the set prefix is ​​used for setter methods or setters. But why don't we use get for getters? The word get in Cocoa has a special meaning: in the name of the Cocoa method, it means that the method returns a value by means of a pointer passed as a parameter. For example, NSData has a getBytes: method.

@ class


Importing a header file establishes a strong dependency between the header file and the source text file that imports it. If the header file is changed, then all files that depend on it need to be recompiled. Sometimes it takes a lot of time. Objective-C allows us to minimize recompiling caused by dependencies. Dependency issues arise because the Objective-C compiler needs certain information to succeed. At times, it requires complete information about the class, such as the layout of the variables of the instance and the chain of its inheritance, and sometimes it is sufficient only the name of the class without its full definition.
The @class is a forward reference. This is a way to tell the compiler to “trust me; you know that this is such a class, and the more you still know and do not. "

Information taken from Objective-C 2.0 and Mac Programming Mark Dalrymple, Scott Knaster .

Source: https://habr.com/ru/post/165989/


All Articles