2. INTRODUCTION
Objective-C was invented by Brad Cox and Tom Love.
Objective-C is a high-level, object-oriented programming language that
adds Smalltalk-style messaging to the C programming language
It is the main programming language used by Apple for the OS X
and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch
Mayank Jalotra
3. INTRODUCTION (Cont.)
Objective-C source files has a “.m” extension.
“.h” file is the interface file.
For example:
─ main.m
─ List.h (Interface of List class.)
─ List.m (Implementation of List class.)
Mayank Jalotra
4. SYNTAX
Objective-C is a thin layer on top of C, and moreover is
a strict superset of C.
It is possible to compile any C program with an Objective-C
compiler.
Objective-C derives its object syntax from Smalltalk.
All of the syntax for non-object-oriented operations are identical to
that of C
Mayank Jalotra
5. Basic syntax structure
C++ syntax :
void function(int x, int y, char z);
Object.function(x, y, z);
Objective-C syntax :
-(void) function:(int)x, (int)y, (char)z;
[Object function:x, y, z];
Mayank Jalotra
6. Keyword: id
The word „id‟ indicates an identifier for an object
much like a pointer in c++.
This uses dynamic typing.
For example, if Pen is a class…
extern id Pen;
id myPen;
myPen = [Pen new ]; Mayank Jalotra
7. MESSAGES
To get an object to do something, you send it a message
telling it to apply a method. In Objective-C, message
expressions are enclosed in square brackets
[receiver message].
In Objective-C one does not simply call a method;
one sends a message
Mayank Jalotra
8. Messages Cont.
For example, this message tells the myRect object
to perform its display method, which causes the
rectangle to display itself
[myRect display];
[myRect setOrigin:30.0:50.0];
Mayank Jalotra
10. CODE EXPLAINATION [1]
CODE EXPLAINATION
The parameter argc is the argument count at the time the
program is invoked. For example, if your program was called
“justarty” and you typed “justarty one two” into the command line
the computer would execute justarty.exe after passing “one” and
“two” into the program.
The value stored in the program for argc would be 3 (including
the program name). The parameter argv on the other hand is the
actual array of arguments. In our example the array of arguments
would be
argv[0] = “justarty”
argv[1] = “one”
argv[2] = “two”
Mayank Jalotra
11. CODE EXPLANATION [2]
NSAutoreleasePool * pool = [[NSAutoreleasePool
alloc] init];
• The NSAutoreleasePool is one of Cocoa's memory-
management tools.
NSLog(@"Hello, World!");
The NSLog function works very much like printf in the C
language. The difference is that NSLog takes an NSString
object instead of a C string. The @" . . . " construct is a
compiler directive that creates an NSString object using
the characters between the quotation marks.
Mayank Jalotra
12. CODE EXPLANATION [3]
[pool release];
• This line contains another part of Cocoa's memory
housekeeping
return 0;
• A return from the main function indicating a normal program
exit
//NS stands for NeXTSTEP. All of the classes and functions in the Cocoa
frameworks start with NS.
Mayank Jalotra
13. Example 1:
#import <Foundation/Foundation.h>
int main(int argc, const char* argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSLog (@”Hello World”);
int undergrads = 120;
int postgrads = 50;
int students = undergrads + postgrads;
NSLog (@”Now featuring...n %i Computer Science students”, students);
[pool drain];
return 0;
}
Mayank Jalotra
14. INTERFACE
The declaration of a class interface begins with the compiler
directive @interface and ends with the directive @end.
@interface ClassName : ItsSuperclass
{
instance variable declarations
}
method declarations
@end
─ Goes in source(.h) file
Mayank Jalotra
15. IMPLEMENTATION
The implementation of the class is done as :
#import <Foundation/Foundation.h>
#include “interfacename.h“
@implementation ClassName
define method function here;
define another method function here;
define yet another method function here;
@end
─ Goes in source(.m) file
Mayank Jalotra
16. Example 2: ComputerScience.h
Example 3: ComputerScience.h
@interface ComputerScience : NSObject
{
int mUndergrads;
int mPostgrads;
}
-(void) print;
-(void) setUndergrads: (int) undergrads;
-(void) setPostgrads: (int) postgrads;
@end
Mayank Jalotra
18. Data Structures
Objective-C arrays are similar to C arrays, but you can initialize whole
array in a list.
Or just a few indices of the array. The rest are set to 0.
Or mix and match; the example will create an array of size [8].
int values[3] = { 3, 4, 2 };
char letters[3] = { 'a', 'c', 'x' };
float grades[100] = {10.0,11.2,1.1};
int array[] = {[3]=11,[2]=1,[7]=0};
Mayank Jalotra
19. Arrays and Functions
Arrays can be passed as arguments to functions.
This function will print every integer in an array but it needs to also be
told how long the array is (arrayLength).
Statements with hard-coded indices such as array[4] are potentially
dangerous.
void function(int array[], int arrayLength) {
int fourthValue = array[4];
for (int i = 0; i < arrayLength; i++) {
NSLog(@”%i”, array[i]; }
}
Mayank Jalotra
20. References
Learning Cocoa with Objective-C By Apple Computer, Inc. , James
Duncan Davidson
Start Programming the Mac Using Objective-C By Bert
Altenberg, Alex Clarke
Objective-C for Absolute Beginners By Gary Bennett, Mitch Fisher, Brad
Lees
Various Authors. Wikipedia: the Free Encyclopedia
http://en.wikipedia.org
http://stackoverflow.com/questions
Mayank Jalotra