Introduction to Objective C             By        Mayank Jalotra
INTRODUCTIONObjective-C was invented by Brad Cox and Tom Love.Objective-C is a high-level, object-oriented programming l...
INTRODUCTION (Cont.)Objective-C source files has a “.m” extension.“.h” file is the interface file.For example:    ─ mai...
SYNTAXObjective-C is a thin layer on top of C, and moreover isa strict superset of C.It is possible to compile any C pro...
Basic syntax structureC++ syntax :  void function(int x, int y, char z);  Object.function(x, y, z);Objective-C syntax : ...
Keyword: id The word „id‟ indicates an identifier for an objectmuch like a pointer in c++. This uses dynamic typing. Fo...
MESSAGESTo get an object to do something, you send it a messagetelling it to apply a method. In Objective-C, messageexpre...
Messages Cont. For example, this message tells the myRect objectto perform its display method, which causes therectangle ...
HELLO WORLD  Hello World                Mayank Jalotra
CODE EXPLAINATION [1]        CODE EXPLAINATION The parameter argc is the argument count at the time the    program is inv...
CODE EXPLANATION [2] NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    • The NSAutoreleasePool is one of Co...
CODE EXPLANATION [3] [pool release];   • This line contains another part of Cocoas memory       housekeeping return 0;  ...
Example 1:#import <Foundation/Foundation.h>int main(int argc, const char* argv[]) {NSAutoreleasePool* pool = [[NSAutorelea...
INTERFACE   The declaration of a class interface begins with the compiler    directive @interface and ends with the direc...
IMPLEMENTATION The implementation of the class is done as :#import <Foundation/Foundation.h>#include “interfacename.h“  @...
Example 2: ComputerScience.h   Example 3: ComputerScience.h@interface ComputerScience : NSObject{   int mUndergrads;   int...
Example 2: ComputerScience.m#include “ComputerScience.h”@implementation ComputerScience-(void) print {int totalStudents = ...
Data Structures Objective-C arrays are similar to C arrays, but you can initialize whole  array in a list. Or just a few...
Arrays and Functions Arrays can be passed as arguments to functions. This function will print every integer in an array ...
ReferencesLearning Cocoa with Objective-C By Apple Computer, Inc. , JamesDuncan DavidsonStart Programming the Mac Using ...
Upcoming SlideShare
Loading in …5
×

Introduction to objective c

333 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
333
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to objective c

  1. 1. Introduction to Objective C By Mayank Jalotra
  2. 2. INTRODUCTIONObjective-C was invented by Brad Cox and Tom Love.Objective-C is a high-level, object-oriented programming language thatadds Smalltalk-style messaging to the C programming languageIt is the main programming language used by Apple for the OS Xand iOS operating systems and their respective APIs, Cocoa and Cocoa Touch Mayank Jalotra
  3. 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. 4. SYNTAXObjective-C is a thin layer on top of C, and moreover isa strict superset of C.It is possible to compile any C program with an Objective-Ccompiler.Objective-C derives its object syntax from Smalltalk. All of the syntax for non-object-oriented operations are identical tothat of C Mayank Jalotra
  5. 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. 6. Keyword: id The word „id‟ indicates an identifier for an objectmuch 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. 7. MESSAGESTo get an object to do something, you send it a messagetelling it to apply a method. In Objective-C, messageexpressions are enclosed in square brackets [receiver message].In Objective-C one does not simply call a method;one sends a message Mayank Jalotra
  8. 8. Messages Cont. For example, this message tells the myRect objectto perform its display method, which causes therectangle to display itself [myRect display]; [myRect setOrigin:30.0:50.0]; Mayank Jalotra
  9. 9. HELLO WORLD Hello World Mayank Jalotra
  10. 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. 11. CODE EXPLANATION [2] NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; • The NSAutoreleasePool is one of Cocoas 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. 12. CODE EXPLANATION [3] [pool release]; • This line contains another part of Cocoas 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. 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. 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. 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. 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
  17. 17. Example 2: ComputerScience.m#include “ComputerScience.h”@implementation ComputerScience-(void) print {int totalStudents = mUndergrads + mPostgrads;NSLog (@”Total students in CompSci= %i”, totalStudents);}-(void) setUndergrads: (int) undergrads { mUndergrads = undergrads;}-(void) setPostgrads: (int) postgrads { mPostgrads = postgrads;}@end Mayank Jalotra
  18. 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. 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. 20. ReferencesLearning Cocoa with Objective-C By Apple Computer, Inc. , JamesDuncan DavidsonStart Programming the Mac Using Objective-C By BertAltenberg, Alex ClarkeObjective-C for Absolute Beginners By Gary Bennett, Mitch Fisher, BradLeesVarious Authors. Wikipedia: the Free Encyclopedia http://en.wikipedia.org http://stackoverflow.com/questions Mayank Jalotra

×