Sunday, June 28, 2009

Objective C - Quick Notes

So, made it through the first 2 chapters on Objective C last night. Here is what I remember. Many of these notes are in relation to java.

General Information



  • Objective C is an extension of C

    • Procedural roots: You don't have to be OO and some libraries like Open GL will be strictly procedural.

    • Pointers: Sort of like references in java. Yeah I know, gross oversimplification. Will elaborate later.

    • Memory management/GC: You have none in iphone apps

    • typedefs: Used to define aliases for types, typically enums

    • You may use C constructs like structs, functions and variables.



  • Objective C is a fully object oriented language

    • encapsulation

    • inheritance

    • polymorphism



  • NSObject is the base object that all Objective-C objects should extend from

  • Classes are objects in and of themselves and operate as factories: You message methods on the class to instantiate objects.
  • There is no concept of a garbage collector, at least on the iphone. You have to handle all memory management.



Data Types

































type modifiers examples string formatting
int
long

long long

short

unsigned

signed


10

010

0xF

15L


%i, %li, %hi

%o, %#o, %lo, %#lo, %ho

%x, %#x, %lx, %#lx, %hx

float

5.5F

1.7e4F


%f, %e, %g
double

5.5

1.7e4

%f, %e, %g
char
'a'
'\n'
%c


C language elements



  • Operators

    • arithmetic, assignment, bitwise and logical operators work like java



  • Flow Control

    • for, while and do while loops work like java

    • continue and break work like java, but don't accept labels

    • goto statement can be used to hit labeled spots.



  • Conditional Statements

    • if/else if/else has same logic & syntax as java.

    • switch/case/default has same logic & syntax as java.

    • ternary operator (condition ? e1 : e2) has same logic & syntax as java

    • Integer expressions are used for boolean values. 0 = false, anything else is true.





Defining Classes - Interface



@interface ClassName : Superclass
memberVariables

methodSignatures
@end


  • This is the interface of the class

  • Typically contained in a .h file

  • Contains

    • member variables

    • method signatures

    • C variables/constants

    • C functions






Defining Classes - Implementation



@implementation [ClassName]
[methodImplementations]
@end


  • This is the implementation of the class

  • Typically contained in a .m file

  • Contains method implementations



Defining Member Variables



Type varname; // variable stored by value
ObjectType *varname; // Pointer to object


Defining Methods



[+|-] (returnType) methodName: (argType)argName {
methodBody
}



  • Method Type

    • + is a class method

    • - is an instance method



  • Return type is in parenthesis and can be a primitive, typedef or class

  • method name uses same conventions as java

  • parameters are specified after a colon. Param type is in parenthesis followed by the name.

  • getter methods follow the convention variableName while setters follow the convention setVariableName



Object Construction



ClassType varName = [[ClassType alloc] init];


  • alloc
    message instantiates the object. Sent to class.
  • init
    message initializes state of object. Sent to object.



Invoking Methods



[receiver message]
[anObject aMethod]
[anObject aMethod:param1:param2]


  • Specify receiver followed by message within brackets

  • Parameters specified after colons



That is what I know for now. I will expand on this later.

No comments: