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.
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.
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.
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.
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:
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.
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
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
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.
Exodai INSTRUCTOR!
Owner and Swift developer!