Dana Vrajitoru
B583 Game Programming and Design
Introduction to Objective C
Objective C
- Object oriented language, based on C with some elements from
Smalltalk. Strongly typed.
- Most of C can still be used in Objective C.
- Used for Mac or iOS apps.
- Current version is 2.0, introduced in 2006.
- All the classes are derived from the same class, id.
- The source code is organized in header files (.h) containing class
interfaces, and .m files containing the implementation.
- Instead of #include, header files are imprted with #import that
automatically prevents multiple inclusions.
Class Interface
Class Implementation
Forward Class Declaration
Public - Protected - Private
- All class methods are public.
- Attributes are protected by default and can be declared as
public/protected/private.
- All class inheritance is public.
- All methods are virtual.
Properties
- The properties are class attributes that Xcode can create set and
get functions for you automatically.
- A property declaration specifies the type of attribute: readwrite,
readonly, etc.
- @synthesize property; - in the implementation, creates the set/get
methods for it.
- retain type of property: the attribute will not be deleted when
the object itself is deleted.
Property Types
- readwrite - set/get function
- readonly - get function only
- copy - the setter function makes a copy of the object that is
referenced by the property.
- assign - the setter function simply assigns the value without
copying it. Used for simple values.
- retain - an additional reference to the property is made so that
the referenced object is not destroyed when the object is deleted.
Class Methods
- The prototype starts with a + or a -.
- Class methods are preceded by +. Constructors are class methods
and have the word alloc in the name.
- Instance methods are preceded by -.
- The return type precedes the name in parenthesis.
- Parameters are given by : (type) name and if more than one is
present, they are preceded by specifiers.
- In the implementation of a method, the pointer to the target
object (this in C++) is identified by self.
Calling Methods
- Calling a class method means sending the object a "message".
- Syntax for calling a method:
[instance/class method];
- If parameters are present, add all their values and the
specifiers:
[instance/class method: arg1 spec: arg2 ...];
- Calling a method from inside a method of the same class requires
the use of self:
[self addX: 2];
Example
@implementation Tile;
- (void) setW: (int) width andH: (int) height
{ ... }
- init // type is (id) if not given
{ ...
return self;
}
main.m:
Tile *t = [[Tile alloc] init];
[t setW: 20 andH: 30];
Interface Builder
- Xcode provides a graphical way to build the interface.
- The Interface Builder allows you to put together the GUI elements
of your app.
- This information is stored in .xib files.
- It allows you to:
- associate classes with certain interface elements;
- link attributes in the code with GUI elements displaying their
values;
- establish callback functions for some events.
Model - View - Controller
- The View is a collection of interface objects, not directly
visible in the code.
- The Controller is a class that links the code and the interface.
- It contains one reference to the model and references to interface
objects.
- The Model is a class that handles the computations, not directly
accessible from the interface.
View - Controller
- In MainMenu.xib you need to declare a generic object of class
Controller.
- The View communicates to the controller through actions which are
class methods in the Controller class:
- (IBAction) myAction: (id) sender;
- The Controller communicates to the View through outlets which are
attributes in the Controller class:
IBOutletNSButton* myButton;
Example IB MainMenu.xib
Examples IB