2. Where we’re headed:
‣Hello World
‣Xcode
‣About Objective-C
‣Syntax
‣Types
‣Operators
‣Classes & Objects Part 1
‣Fraction Calculator App
3. The Fraction Calculator
‣ Work with basic data
types
‣ Work with simple
objects and a simple
object model
‣ Basic MVC architecture
‣ Build a basic UI with
interface builder
12. What is Objective-C
‣ An orthogonal superset over C
‣ Orthogonal = doesn’t override any C
functionality (almost)
‣ Object oriented
‣ Syntax a mix of SmallTalk and C
‣ Can be statically or dynamically typed
13. Objective-C Filetypes
‣ .xcodeproj - Your project bundle
‣ .h - header file
‣ .c - a C source file
‣ .m - an Objective-C source file
‣ .plist - a property list file
14. Syntax
‣ All statements end with a semi-colon
‣ Blocks (lower case ‘b’) are denoted by curly braces
‣ Comments:
‣ // in-line comment
‣ /* . . . . */ block comment
‣ # pre-processor directive
15. Syntax
Function Definition
int cube (int num)
{
return num * num;
}
Function Calls
cube (10);
Declaring Variables
int value = 10;
16. C Types
‣ Basic Types
‣ Int - an integer
‣ Float - decimal number
‣ Double - double precision decimal number
‣ Char - a single ASCII character (1 byte)
‣ Void - nothing (a variable cannot be declared void)
‣ Variations
‣ Signed / Unsigned (for ints)
‣ Long, long long (32 or 64 bits)
‣ Short (16 bits)
‣ Casting
‣ ( type ) variable
17. Objective-C Additions
‣ BOOL - boolean
‣ YES or NO
‣ id - pointer type for an objective-c object
‣ nil - the null value for objective-c
18. Esoteric Types
Struct
struct CGPoint {
CGFloat x;
CGFloat y;
};
Union
union aNumber {
int i;
float f;
}
Enum
enum CGRectEdge {
CGRectMinXEdge,
CGRectMinYEdge,
CGRectMaxXEdge,
CGRectMaxYEdge
};
19. Esoteric Types
Basic Typedef
typedef long NSInteger;
With a struct or enum
typedef struct {
CGFloat x;
CGFloat y;
} CGPoint;
21. Operators
‣ Assignment
‣ = simple assignment
‣ += add to
‣ -= subtract from
‣ *= multiply by
‣ /= divide by
22. Operators
‣ Logical
‣ == - equal to
‣ != - not equal to
‣ <, > - less than, greater than
‣ <=, >= - less than or equal, greater than or equal
‣ && - logical AND
‣ || - logical OR
‣ Pointers and References
‣ * - dereference
‣ & - reference (address of)
23. OO Syntax
‣ The ‘@’ symbol
‣ Used to create NSStrings
‣ Used as a prefix to obj-c keywords
‣ Calling Object Methods
[[NSAutoreleasePool alloc] init];
[NSDictionary dictionaryWithObject:@"foo" forKey:@"bar"];
24. Loops
For loop
for (int i = 0; i < 100; i++)
{
/* statements */
}
While loop
while (condition)
{
/* statements */
}
Do ... While loop
do {
/* statements */
} while (condition);
25. Branching
If ... else if ... else
if (condition)
{
/* statements */
}
else if (anotherCondition)
{
/* statements */
}
else
{
/* statements */
}
26. Branching
Switch
switch (expression)
{
case aConstant:
/* statements */
break;
case anotherConst:
/* statements */
break;
default:
/* statements */
break;
}
Ternary Operator
int value = condition ? 10 : 20;
29. A Car Object
‣ Properties
‣ Make
‣ Model
‣ Year
‣ Color
‣ Actions
‣ Accelerate
‣ Lock
‣ Steer
30. Objective-c Objects
‣ Data and Interface
Data Interface Message
‣Messages are sent to Objects
‣Objects may respond to messages
31. Messaging
‣ Messages are sent to objects
‣ Different than calling a method
‣ Messages are referred to as selectors
‣ The object knows its type and will respond
accordingly.
‣ All selectors are “virtual”
‣ Objects may respond to messages
‣ If the selector doesn’t exist (is unrecognized) an
exception is typically thrown.
32. Sample Class Interface
@interface MyClass : NSObject
{
int value
id someData
NSString *name
}
- (id)initWithName:(NSString *)name;
+ (MyClass *)createWithName:(NSString *)name;
@end
34. Named Properties
Named Property Declaration
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int value;
Interface Implementation
@synthesize name, value;
At compile time, the @synthesize produces getters
and setters.
35. Sample Class Interface
@interface MyClass : NSObject
{
int value
id someData
NSString *name
}
- (id)initWithName:(NSString *)name;
+ (MyClass *)createWithName:(NSString *)aname;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int value;
@end
55. View Controller
‣ Subclass of UIViewController
‣ Properties
‣ View
‣ Actions
‣ Respond to UI Events
‣ Manipulate UI Elements
‣ As a subclass of UIViewController, many functions
are provided already.