Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

An Overview of Objective-C - Part 1

DZone's Guide to

An Overview of Objective-C - Part 1

·
Free Resource

I’ve been blogging about our educational iPhone/iPod Touch RPN calculator application called CrunchTime, and so far have given an overview of the platform, talked about the vision of the project, discussed the deployment process of an iPhone application, introduced the Xcode IDE, and outlined the functional specifications of our project.

As I stated before, the main programming language for development on Apple platforms is Objective-C and in a few blog posts I’m going to give a quick overview of the main features of Objective-C that was the main thing I needed to get started with this language for our project. I’m going to use a simplified version of the code that I wrote for the calculation engine of CrunchTime to showcase the features in my examples.

Objective-C is an Object-Oriented programming language that adds a Smalltalk style of messaging to the C programming language. The syntax is a combination of original C syntax combined with Smalltalk message style even though in the newer versions some of the common syntax styles are included to make it easier for C developers to learn, use, and adapt the language.

Like C, you have multiple files for the implementation of a class in Objective-C: a header file that includes the declaration and abstractions of the class with .h extension, and an implementation file with definitions and .m extension. In this part I focus on header files.

The following code is the header file for the CalculationEngine class in CrunchTime named CalculationEngine.h.

#import <Foundation/Foundation.h>
02.#import "StackManager.h"
03.
04.@interface ComputationEngine : NSObject
05.{
06. StackManager *mgr;
07.}
08.
09.// Initialize the stack
10.-(id) init;
11.// Calculate the result of an operation by getting the operands from the stack
12.-(double) calculate: (NSString*) operator;
13.// Push an operand to the stack
14.-(void) pushOperand: (double) v;
15.// Pop an operand from the stack
16.-(double) popOperand;
17.// Get a pointer for the stack
18.-(NSArray*) getStack;
19.// Get the current size of the stack
20.-(int) getSize;
21.// Check to see if stack is empty
22.-(BOOL) isEmpty;
23.// Check to see if stack is full
24.-(BOOL) isFull;
25.// Clear the stack
26.-(void) clear;
27.// Peek a value on stack
28.-(double) peek;
29.@end

I explain main parts of this class declaration in the next few sections.

References

As you see, there are a couple of library imports from external sources using the import keyword and the same syntax that you have in C or C++.

#import <Foundation/Foundation.h>
2.#import "StackManager.h"

Here I’ve imported the Foundation library available in Cocoa and the StackManager class written by one of my teammates for stack operations.

Interface

There is an interface definition for the class starting with @interface and ending with @end.

@interface ComputationEngine : NSObject
2.
3.@end

This specifies the superclass as NSObject, and can include the declarations of methods for the class. NSObject is the root object in Objective-C hierarchy but you can replace it with any other class based on your needs.

Field and Property

You can also define fields and properties that are accessible within the class scope.

@interface ComputationEngine : NSObject
2.{
3. StackManager *mgr;
4.}
5.@end

Here I’ve defined a pointer reference to StackManager class which is another internal class in our application responsible for stack operations. This pointer is an accessible field in the ComputationEngine class, and in the next part I’ll use it to have easy access to stack operations in my implementation file.

Method Declaration

The method declarations are straightforward.

// Initialize the stack
02.-(id) init;
03.// Calculate the result of an operation by getting the operands from the stack
04.-(double) calculate: (NSString*) operator;
05.// Push an operand to the stack
06.-(void) pushOperand: (double) v;
07.// Pop an operand from the stack
08.-(double) popOperand;
09.// Get a pointer for the stack
10.-(NSArray*) getStack;
11.// Get the current size of the stack
12.-(int) getSize;
13.// Check to see if stack is empty
14.-(BOOL) isEmpty;
15.// Check to see if stack is full
16.-(BOOL) isFull;
17.// Clear the stack
18.-(void) clear;
19.// Peek a value on stack
20.-(double) peek;

Method declarations start either with + or –:

  • A single plus before the method name makes it an instance method. In this case you need an object instance of this class to have access to the method.
  • A single dash before the method name makes it a class method. In this case you can call the method by using the class name only. This is roughly similar to static methods in C#.

The return type of a method is embedded within parentheses and the name of of the method is followed after a space.

The list of parameters come after the method name after a colon. Multiple parameters can be separated with colons. The type of the parameter should be embedded within parenthesis and the name should follow after the space.

As a side-note, you may have noticed that you can insert code comments with the same syntax as C, C++, C#, or Java.

In the next part in this series I’ll describe the implementation file which contains more details about basic Objective-C syntax.

 




Topics:

Published at DZone with permission of Keyvan Nayyeri. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}