Objective-C was written in the early 1980s. It was designed as a way of introducing the capabilities of the Smalltalk language into a C programming environment. A majority of the iPhone’s framework libraries are written in Objective-C, but because the language was designed to accommodate the C language, you can use C and C++ in your application as well. Objective-C is used primarily on Mac OS X and GNUstep (a free OpenStep environment). Many languages, such as Java and C#, have borrowed from the Objective-C language. The Cocoa framework makes heavy use of Objective-C on the Mac desktop, which carried over onto the iPhone.
If you’ve developed on the Mac OS X desktop before, you’re already familiar with Objective-C, but if the iPhone is your first Apple platform, then you’re likely transitioning from C or C++. In this section, we will try to cover some of the more significant differences between these languages.
The first thing you’ll notice in Objective-C is the heavy use of brackets. In Objective-C, methods are not called in a traditional sense; instead, they are sent messages. Likewise, a method doesn’t return, but rather responds to the message. Unlike C, where function calls must be predefined, Objective-C’s messaging style allows the developer to dynamically create new methods and messages at runtime. The downside to this is that it’s entirely possible to send an object a message to which it can’t respond, causing an exception and likely program termination.
Given an object named myWidget, a message can be sent to its powerOn method this way:
returnValue = [ myWidget powerOn ];
The C++ equivalent of this might look like:
returnValue = myWidget->powerOn( );
The C equivalent might declare a function inside of its flat namespace:
returnValue = widget_powerOn(myWidget);
Arguments can also be passed with messages, provided that an object can receive them. The following example invokes a method named setSpeed and passes two arguments:
returnValue = [ myWidget setSpeed: 10.0 withMass: 33.0 ];
Notice the second argument is explicitly named in the message. This allows multiple methods with the same name and data types to be declared-polymorphism on steroids.
returnValue = [ myWidget setSpeed: 10.0 withMass: 33.0 ];
returnValue = [ myWidget setSpeed: 10.0 withGyroscope: 10.0 ];
Declarations of Class and Method
While C++ classes can be defined in Objective-C, the whole point of using the language is to take advantage of Objective-C’s own objects and features. This extends to its use of interfaces. In standard C++, classes are structures, and their variables and methods are contained inside the structure. Objective-C, on the other hand, keeps its variables in one part of the class and methods in another. The language also requires that the interface declaration be specifically declared in its own code block (called @interface) separate from the block containing the implementation (called @implementation). The methods themselves are also constructed in a Smalltalk-esque fashion, and look very little like regular C functions.
An example of an interface of a widget might look like this:
@interface MyWidget: BaseWidget
@private float speed;
@protected float mass;
@protected float gyroscope;
- (void)setSpeed:(float)_speed withMass:(float)_mass;
- (void)setSpeed:(float)_speed withGyroscope:(float)_gyroscope;
Each of the important semantic elements in the example above is explained in the following sections.
The preprocessor directive #import replaces the traditional #include directive (although #include may still be used). One advantage to using #import is that it has built-in logic to ensure that the same resource is never included more than once. This replaces the round-about use of macro flags found routinely in C code:
The interface is declared with the @interface statement followed by the interface’s name and the base class (if any) it is derived from. The block is ended with the @end statement.
Methods are declared outside of the braces structure. A plus sign (+) identifies the method as a static method, while a minus sign (-) declares the method as an instance method. Thus, the alloc method (to allocate a new object) will be called using a reference directly to the MyWidget class, whereas methods that are specific to an instance of the MyWidget class, such as needsBatteries and powerOn, will be invoked on the instance returned by alloc.
Every declared argument for a method is represented by a data type, local variable name, and an optional external variable name. Examples of external variable names in the above example are withMass and withGyroscope. The notifier (calling function) that invokes the method refers to external variable names, but inside the method the arguments are referenced using their local variable name. Thus, the setSpeed method uses the local _mass variable to retrieve the value passed as withMass. If no external variable name is supplied in the declaration, the variable is referenced only with a colon,
This brief overview simply sets out to summarise a few examples of the difference in flavour of Objective C programming as contrasted with the other more widely used variants. For further information there is nothing as thorough as the Apple developers websites as well as a host of detailed manuals produced by third party authors.
Fancy joining the goldrush that is riding on the success of iPhone’s Apps?
This is prime time to join in as the iPad comes on stream, giving another niche for your creative talents to exploit
Article Source: http://EzineArticles.com/?expert=Ray_Cassidy
Latest posts by Alex Shaw (see all)
- The Importance Of Objective C in iPhone and IDE Applications - October 25, 2011
- Amazon EC2 Paves the Way in Hosting - October 17, 2011
- New Improvements in Developer Tools in Linux Programming - October 12, 2011