The structure of Objective C explained

In comparison to Swift, Objective C has a project structure that needs a little bit of explaination. In this article, we try to explain how the structure, project structure and coding structure is managed and organized.

article

In comparison to Swift, Objective C has a project structure that needs a little bit of explaination. In this article, we try to explain how the structure, project structure and coding structure is managed and organized.


The structure of Objective C.


Understanding the structure of Objective-C code is essential for any iOS developer, especially when dealing with legacy projects. Objective-C, like many other programming languages, organizes its code in a specific manner to enhance readability, maintainability, and functionality. This article will delve into the fundamental components of Objective-C's structure, including header files (.h), implementation files (.m), and various other important elements such as imports, interfaces, and implementations.


Header Files (.h)


In Objective-C, header files are used to declare the interface of a class or a set of functions. These files have the .h extension and are essential for defining the structure of your classes, including their properties and methods.


Example of a Header File


Here’s a simple example of a header file for a class named Person:


    
// Person.h

#import 

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

- (void)greet;

@end
    

Let me explain:


  • #import : imports the Foundation framework, which provides essential data types, collections, and operating system services.
  • @interface: declares the interface of the Person class.
  • @property: defines two properties: name (an NSString object) and age (an NSInteger).
  • - (void)greet; : declares a method that the class will implement.

Implementation Files (.m)


Implementation files, with the .m extension, contain the actual implementation of the methods declared in the header files. These files define how the methods and properties behave.


Example of a Implementation File


Continuing from the previous example, here’s the implementation file for the Person class:


    
// Person.m

#import "Person.h"

@implementation Person

- (void)greet {
    NSLog(@"Hello, my name is %@ and I am %ld years old.", self.name, (long)self.age);
}

@end
    

  • #import : imports the UIKit framework, necessary for building graphical user interfaces on iOS.
  • #import "Person.h": imports the Person class, allowing its use within the file.

Interface and Implementation


The structure of an Objective-C class is divided into two main parts: the interface and the implementation


The interface part is defined in the header file and declares the properties and methods that the class provides.


    
@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

- (void)greet;

@end
    

The implementation part, found in the .m file, provides the code that makes the methods work.


    
@implementation Person

- (void)greet {
    NSLog(@"Hello, my name is %@ and I am %ld years old.", self.name, (long)self.age);
}

@end
    

Conclusion


As you can see, the structure of Objective C is very different from, for example, Swift. That's why we wanted to dedicate a separate article to it before we continue learning other concepts in Objective C such as Variables, Methods, Classes and Protocols.

instructor

Exodai INSTRUCTOR!

Johan t'Sas

Owner and Swift developer!