Objective-C
Objective-C is a general-purpose, high-level language that added object-oriented programming abilities to the existing C language. It was created in the 1980's, and was selected as the primary language of the NeXTSTEP operating system. NeXT was the company that Steve Jobs joined after being ousted from Apple. Eventually, Apple purchased NeXT and Jobs was put back in as CEO. This is how Objective-C entered the Apple ecosystem. NeXTSTEP became what is known today as macOS 10 (or X as they called it).
Many of the functions and libraries that are in use for Objective-C still contain the characters "NS" at the beginning of their names. For instance, NSLog, NSApplication, NSCopy, etc. These were created at NeXT.
Objective-C Code Samples
Below are basic code examples demonstrating various Objective-C concepts like classes, objects, methods, and more. Objective-C is a superset of C, adding object-oriented features and dynamic runtime.
Defining a Class
In Objective-C, a class is defined in two parts: the interface (@interface) in the .h (header) file and the implementation (@implementation) in the .m (implementation) file.
MyClass.h
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
// Property declaration
@property (nonatomic, strong) NSString *name;
// Method declarations
- (void)printGreeting;
+ (void)classMethodExample;
@end
MyClass.m
#import "MyClass.h"
@implementation MyClass
// Synthesize properties
@synthesize name = _name;
// Implement the printGreeting method
- (void)printGreeting {
NSLog(@"Hello, %@!", self.name);
}
// Implement the class method
+ (void)classMethodExample {
NSLog(@"This is a class method.");
}
@end
Creating and Using Objects
Once you have a class, you can create and use objects from that class.
#import **\<Foundation/Foundation.h\>**\
#import **\"MyClass.h\"**\
int main(int argc, const char \* argv\[\]) {\
\@autoreleasepool {\
*// Create an instance of MyClass*\
MyClass \*myObject = \[\[MyClass alloc\] init\];\
\
*// Set the name property*\
myObject.name = @\"World\";\
\
*// Call the instance method*\
\[myObject printGreeting\];\
\
*// Call the class method*\
\[MyClass classMethodExample\];\
}\
**return** 0;\
}
Working with Methods
Objective-C differentiates between instance methods and class methods. Instance methods operate on an object and use a - prefix, while class methods use a + prefix and can be called on the class itself.
Instance Method:
- (void)printGreeting {
NSLog(@"Hello, %@!", self.name);
}
Class Method:
+ (void)classMethodExample {
NSLog(@"This is a class method.");
}
Implementing Initializers
Initializers in Objective-C are instance methods used to set up an object's initial state. The most common initializer pattern is overriding init.
- (instancetype)init {
self = [super init];
if (self) {
_name = @"Default Name";
}
return self;
}
Protocols
Protocols define a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.
Defining a Protocol:
@protocol MyProtocol <NSObject>
- (void)requiredMethod;
@optional
- (void)optionalMethod;
@end
Implementing a Protocol:
@interface MyClass () <MyProtocol>
@end
@implementation MyClass
- (void)requiredMethod {
NSLog(@"This is a required method.");
}
- (void)optionalMethod {
NSLog(@"This is an optional method.");
}
@end
These are some basic Objective-C syntax and concepts, to get you started to properly debugging an application.