SlideShare uma empresa Scribd logo
1 de 20
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 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
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
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
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
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
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
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
HELLO WORLD
  Hello World




                Mayank Jalotra
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
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
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
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
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
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
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
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
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
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
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

Mais conteúdo relacionado

Mais procurados

Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Chris Richardson
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On RandomnessRanel Padon
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - CJussi Pohjolainen
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingfarhan amjad
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 

Mais procurados (19)

Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
All experiment of java
All experiment of javaAll experiment of java
All experiment of java
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 

Semelhante a Introduction to objective c

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developersgeorgebrock
 
Objective c
Objective cObjective c
Objective cStijn
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java DevelopersMuhammad Abdullah
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to DistributionTunvir Rahman Tusher
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guideTiago Faller
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdfamitbhachne
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2NAILBITER
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]Palak Sanghani
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
 

Semelhante a Introduction to objective c (20)

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Objective c
Objective cObjective c
Objective c
 
Objective c intro (1)
Objective c intro (1)Objective c intro (1)
Objective c intro (1)
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
First fare 2010 java-introduction
First fare 2010 java-introductionFirst fare 2010 java-introduction
First fare 2010 java-introduction
 
Objective c
Objective cObjective c
Objective c
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
 
Why MacRuby Matters
Why MacRuby MattersWhy MacRuby Matters
Why MacRuby Matters
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to Distribution
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2
 
EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 

Introduction to objective c

  • 1. Introduction to Objective C By Mayank Jalotra
  • 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
  • 9. HELLO WORLD Hello World 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
  • 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. 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