Introduction
Share:
Objective-C is a high-level programming language that was developed by Brad Cox and Tom Love in the early 1980s at Stepstone. It was adopted as the primary language used by NeXT for its NeXTSTEP operating system (OS), from which macOS and iOS are derived. As such, it has been extensively used to write software for Apple’s desktop and mobile OSs.
Objective-C extends the C programming language by adding object-oriented programming (OOP) capabilities. It strikes a remarkable balance between the power of lower-level languages like C and the ease-of-use and safety of higher-level languages. In particular, its dynamic typing and message passing features allow highly flexible, adaptable, and decoupled software design. Now, let's dive deeper into understanding the Objective-C language.
Objective-C is a superset of the C programming language. This means that any valid C code can be inserted into an Objective-C class with little difficulty. Meaning, developers are not constricted by the limitations of C when coding in Objective-C. Developers benefit from the flexibility in selecting available tools for a specific task at hand—be it utilizing the simplicity of C, or the power of C++ where required.
Consider the following example:
#include <stdio.h>
int main() {
//C-code
printf("Hello, World! \n");
return 0;
}
The above is an example of a C program that Objective-C treats as completely valid, compilable, and executable. The printf function is invoked to print "Hello, World!" to the console or terminal.
Objective-C also includes several keywords to its vocabulary that are added to C - @class, @public, @protected, @private, @property, @end, @implementation, @synthesize, @dynamic, @try, @throw, @catch, @finally, and @protocol, to name a few.
Let's get an insight into Object-Oriented Programming with Objective-C. Objective-C is an object-oriented language, and object-oriented programming is a method for organizing complex programs. In object-oriented programming, programmers can think about programming problems in terms of objects and actions that happen to them.
In Objective-C, we write programs like we're telling a story. We have:
- Nouns (objects)
- Verbs (methods)
- Descriptions (attributes)
@interface SampleClass:NSObject
- (void)sampleMethod;
@end
@implementation SampleClass
- (void)sampleMethod {
NSLog(@"Hello, World! \n");
}
@end
This is a simple Objective-C class (SampleClass) that inherits from a superclass (NSObject) and declares a single instance method (sampleMethod). The NSLog
method is invoked to print "Hello, World!" to the console or terminal.
In Object-Oriented Programming, an Interface is like a contract, or specification, that a class agrees to follow. If a class defines an interface, it's saying, "I guarantee to have these methods and properties."
The @interface directive begins the declaration of a class or a category. NSObject
signifies that SampleClass is a subclass of NSObject. This provides SampleClass with the ability to behave like other Objective-C objects.
Next comes the @implementation
directive. Here it marks the start of the class's implementation. Methods declared in the interface file are defined in this file.
Let's talk about variables and data types in Objective-C. Like many programming languages, Objective-C has several built-in data types. The common data types are int, float, char, id, etc.
You'll notice that instead of using int or string, Objective-C has different object-oriented versions of these present in the Foundation Framework, such as NSNumber, NSString, NSDate, NSArray, and NSDictionary. The id
data type is a special type in Objective-C. It can be used to store a reference to any type of object.
This is a basic introduction to Objective-C. As with any programming language, the best way to master Objective-C is by coding, trial and error, and gaining actual experience. Objective-C has a steep learning curve, but it’s entirely worth climbing. Remember, even faulty code is worthwhile, as it helps us understand what not to do. Keep practicing and happy coding!
0 Comment
Sign up or Log in to leave a comment