prevents initialization loops with using inheritance. This is the initilizer
that a child class should use to initilize its superclass instance variables,
it is defined in the documentation of the class
Back
class implementation
Front
// implementaitons are stored in .m files
// add private methods to implementation file
#import "ClassInterface.h"
@implementation className // no superclass here
// implementation of pubic and private mentods
@end
Back
Instantiation
Front
Creating an Object
- objects can be created manually
- or they can be created by other objects
- "most of the objects you get by passing other objects"
Back
pointers and nil
Front
Classes are always on the heap in Objective C
when a class pointer is defined it is set to nil
if you pass a method to a nil object NOTHING HAPPENS,
this is an error in some languages.
Back
for loop, while loop, switch
Front
same as C++
while loop has
continue; // just breaks out of that cycle
Back
id
Front
a very important generic class, it is similar to void* but has useful methods
associated on top of it. It can hold the pointer of any class.
Back
Class Methods
Front
// prototype
+ (returnType)methodName;
//Calling a class method
[ClassName method:arg]
//Can get the class of an object and call a class method as follows
[[instanceName class] classMethod];
// Class methods are typically used to get information about the class
Back
Relational Operators
Front
Same as C++
<, >, >=, <=, !=, ==
Back
self
Front
pointer to the object calling the method (like this) use it whenever you are
manipulating an instance variable, it helps with code readability.
Back
main syntax
Front
int main(int argc, const char* argv[]){
//code
return 0; // if sucessful
}
Back
private methods in implementation file
Front
// at the top of the implementation add an interface
@interface ClassName() // don't for get the () and no Superclass here
// declaration of private methods here (as needed)
@end
Back
Dynamic Binding
Front
in Objective-C the decision of what code is ran happens entirely at runtime. You
can assign id to any class and access the classes methods even though the class is
an id. The compiler assumes you know what you are doing if you are sending a
message to an id.
example:
id obj = ....;
[obj shoot]; //will compile, even if the object that obj was originally assigned to
// does not contain a method called shoot
Back
class interfaces
Front
// interfaces are stored in .h files and have the following syntax
#import "Superclass.h" // if the class has a superclass
@interface ClassName : Superclass
// Declare methods hear
@end
Back
standard libraries
Front
Found in <Foundation/Foundation.h>
i.e. #import <Foundation/Foundation.h>
standard liberties are in <>
and other headers are in ""
Back
method implementation
Front
- (returnType)functionNameA:(Arg1Type)arg1Name functionNameB:(Arg2Type)arg2Name
{
// put the code here to do the function
}
// no semicolon
// it is all on one line this time
Back
Logical Operators
Front
Same as C++
&&, ||, !
Back
Introspection
Front
When id is used with an array to store many objects of different types.
Back
Conditional Operands
Front
variable = (condition) ? AssignValue1: AssignValue2;
// if the variable is equal to the condition then AssignValue1 is
// is used if not then AssignValue2 is used.
Back
method prototype syntax
Front
// it includes the -
- (returnType)functionNameA:(Arg1Type)arg1Name
functionNameB:(Arg2Type)arg2Name;
// can all be on one line
// try to line up the colons
// the full name of the method is fuctionNameA:FunctionNameB:
// See the following example
- (void)orbitPlanet:(Plant *)aPlanet
atAltitude:(double)km;
// the name of this function is orbitPlanet:atAltitude:
Back
@synthesize
Front
// implements both the setter and getter for a @property
// AND creates some storage to hold the value of the instance variable
@synthesize nameOfProperty = _nameOfproperty;
// this is the name of where the value is stored
// if not the name will be nameOfProperty
// apparently we always want to use = with @synthesize
// to do more, synthesize only implemets the setters and
// getters the you do not
Back
arithmetic and type casting
Front
same a C++
i.e. int Number = (int)(3.14*42.3);
Back
standard init implementation
Front
@implementation MyObject
- (id)int
{
self = [super init]; //call supercalss initilizer
if(self){
// initilize class here
}
return self;
}
@end
all other initializers should call the designated initializer
Back
Setting and Getting
Front
// methods used to set and get a primitive data type
// a simple method used to set a value, the implemention just sets ValueA to NewValueA
- (void)setValueA:(double)NewValueA;
// a get method simple returns the value
- (double) ValueA;
// @property is used to make this easier
Back
Creating an object from scratch
Front
-alloc is a class method of NSObject that allocates space in the heap
large enough to hold all of the instance variables of the class that
receives a message.
-the instance variable will be initillally set to zero, if you want
the to be otherwise use the init method to set the values.
[[MyClass alloc] init] will do the job
Back
Calling a method
Front
[classPointer method];
or
classPointer.Method // equivalent to classPointer->Method in C++
the compiler knows if you are calling a setter or a getter
Back
@property
Front
//declares a setter and a getter for a property
//syntax
@property (nonatomic) double nameOfProperty;
// this makes the setNameOfProperty and nameOfProperty methods
// nonatomic makes the property thread safe
// notice that the property name starts with a lowercase
// but the setter have a uppercase name
// if you create a class property use strong for memory management
@property (nonatomic, strong) ClassName *nameOfInstance;
Back
NSObject
Front
All objects inherited from NSObject, some useful methods are:
isKindOfClass; //tells you if the class is of class(inheritance included)
isMemberOfClass; // inheritance not included
responds ToSelector: //returns weather an object responds to a given method
// must use selectors with these objects
Back
selector
Front
a special object used testing objects i.e. using NSObject methods
@selector() turns the name of a method into a selector
example: @selector(shoot:Atpostion:)
SEL shootSelector = @selector(shoot:);
//SEL is a typedef that holds a selector.
Back
initializer
Front
(id)init; or (id)initWithProperty:(double)property;
this is like the C++ constructor, most classes should implement init
sensibly, you can have as many inits as you like using initWithProperty,
this is similar to a constructor with arguments
Back
public and Private
Front
in objective C, anything that is in the .h (interface) file
is public and anything that is in the .m (implementation)
file is private
Back
#import
Front
Similar to #include, however takes care of redundancy (i.e. no #ifndef)