2. Outline
• From C to Objective-C
• OO in Objective-C
• Memory management
• Demo iOS
2
3. History, in brief
• Created in the early ‘80s by Brad Cox and
Tom Love
• First Obj-C runtime in 1992
• Used by NeXT in the 90’s, later acquired by
Apple
• Apple introduced Obj-C 2.0 in 2006
3
4. A C superset
• Obj-C adds Smalltalk-style messaging to C,
used for OO operations
• C code is completely supported and used
for any non OO operation
• Different philosophy w.r.t. C++
4
5. Obj-C vs C++
• C++ adds OO-programming, generic
programming and metaprogramming to the
C language
• Obj-C adds OO-programming, dynamic
typing and reflection
• Bottom line C++ is geared toward
compile-time features, whereas Obj-C is
geared toward run-time features
5
6. A remarkable quote
“I made up the term ‘object-oriented’ and I
can tell you I did not have C++ in mind”
(Alan Kay, 1997)
6
7. Obj-C vs C (strings)
• C is fully supported, but some constructs are
seldom used
• Strings are one example
C string: “string”
Obj-C string: @“string”
• Notice the @ sign
7
8. Pointers,
pointers everywhere
• Typical C pointer usage
int* intPtr;
/* stuff */
int anInt = *intPtr //dereferencing
• Usually pointers are dereferenced and we
speak about the actual objects they point
8
9. Pointers,
pointers everywhere
• In Obj-C that’s not the case: we declare
pointers and we treat them as actual objects
• Anything you’ll want to do in Obj-C with an
object will expect a pointer
• Obj-C itself will take care of accessing the
actual objects under the hood.
9
10. Pointers,
pointers everywhere
NSString * s = @”Hi there”
• That’s convenient and that’s why we’ll tend
to say that “s is an NSString” when it is
actually a pointer to it
• But, please, never forget that a pointer is a
pointer!
10
11. Pointers may trick you
o1 o2 o1 o2
ptr1 = ptr2
ptr1 ptr2 ptr1 ptr2
• A typical beginners’ mistake is to think that
the above assignment will provide a copy of
o2, which is obvious not true since we’re
assigning a pointer
11
12. Obj-C Objects Syntax
• The Obj-C syntax derives from Smalltalk
• Messages are sent to objects with a square
brackets syntax, like for example
[myObject doThis]
an instance a message
12
13. Message parameters
• A message can have parameters, of course
[myObj doWithPar:par1
otherPar:par2]
• The corresponding method’s signature will
be
doWithPar:otherPar:
13
14. Overloading
• Obj-C does not support overloading
• That’s not a big deal since methods use the
infix notation
• The name of the method is mixed with it’s
arguments
• This increases verbosity but also clarity
14
15. Writing vs Reading
Peter Hallam (Microsoft): What Do Programmers Really Do Anyway?
http://blogs.msdn.com/b/peterhal/archive/2006/01/04/509302.aspx
15
16. Java vs Infix notation
• A real method call from an Android app
PendingIntent.getActivity(context, 0, new
Intent(), 0);
• In Obj-C it would look something like
[PendingIntent activityWithContext:context
requestCode:0
intent:[Intent new]
flags:0];
16
17. Java vs Infix notation
• A real method call from an Android app
PendingIntent.getActivity(context, 0, new
Intent(), 0);
dafuq is this?!
• In Obj-C it would look something like
[PendingIntent activityWithContext:context
requestCode:0
intent:[Intent new]
flags:0];
16
18. Java vs Infix notation
• A real method call from an Android app
PendingIntent.getActivity(context, 0, new
Intent(), 0);
dafuq is this?!
• In Obj-C it would look something like
16
19. Java vs Infix notation
• A real method call from an Android app
PendingIntent.getActivity(context, 0, new
Intent(), 0);
dafuq is this?!
• In Obj-C it would look something like
[PendingIntent activityWithContext:context
requestCode:0
intent:[Intent new]
oh, a request code, I see...
flags:0];
16
20. Nesting calls
• Of course calls can be nested
[politeObject sayHiTo:[anOtherObj name]];
[[MyClass alloc] initWithName:[foo name]];
17
21. The nil case
• A non-valid object pointer has value nil
• Almost the same as a NULL pointer
• It is a form of zero, therefore the following
code is (ugly but) legal
obj = nil;
if(obj) { /% do stuff %/ }
18
22. Talking to nil
• Any Java programmer here? Do you love
NullPointerExceptions?
• In Obj-C there no such thing! That’s because
sending a message to nil is legal
• What’s the value of obj2 after this code?
obj1 = nil;
obj2 = [obj1 doSomething];
19
23. Talking to nil
• Whether this is a good or a bad thing is a
quasi-religious issue
• Cons: may cause silent failures difficult to
track down
• Pros: allows flexibility
20
24. A touch of Class
• Classes are generally divided into two
chunks of code
@interface MyClass defined in
@end MyClass.h
@implementation MyClass defined in
@end MyClass.m
21
25. Inheritance
• The @interface declaration allows to
specify a parent class
@interface MyClass : NSObject
• NSObject is the Cocoa- base class- but it is
(actually another one exists NSProxy
not of our interest)
22
26. Methods declaration
• Methods are declared as follows
@interface MyClass : NSObject
+ (MyClass *)myClassInstance;
- (NSString *)sayHelloClass;
@end
• Class methods starts with the + sign
• Instance methods with the - sign
23
27. Methods definition
• Methods are then defined in the
@implementation section of the class
@implementation MyClass
- (NSString *)sayHelloClass {
return @”Hello Class!”;
}
...
@end
24
28. Instance variables
• Instance vars are traditionally declared in
the @interface section.
• However since iOS 5 it is allowed to
declare them in the @implementation
• Consider that the @interface section is
usually visible to other classes
25
29. Instance variables
• Here’s an example of ivars declaration
@interface MyClass : NSObject {
NSInteger anInteger;
NSString * aString;
}
@end
26
30. Properties
• A property is a syntactical feature of Obj-C
2.0, i.e. syntactic sugar for calling an accessor
method. Example:
NSString * name = [aPerson name];
[aPerson setName:@”Mary”];
equivalent to
NSString * name = aPerson.name;
aPerson.name = @”Mary”;
27
31. Properties declaration
• A property is generally declared in the
@interface section as follows (< iOS4
style)
@property(nonatomic, retain)this changed *
NSString
* since iOS5
name;
• The above line declares the accessor methods for
the name variable.
• The options in the parenthesis define the
28
32. Properties definition
• The actual implementation of accessor
methods is achieved like follows:
@implementation MyClass
@synthesize name;
...
• The @synthesize keyword provides the
implementation of the two methods
- (NSString *)name
- (void)setName:(NSString *)aName
29
38. Memory Management
alloc retain count I care!
1 2
I care! I don’t care any longer
I care! me neither 1
5
I care! neither do I neither do I
30
39. Memory Management
alloc retain count I care!
1 2
I care! I don’t care any longer
I care! me neither 1
5
I care! neither do I neither do I
I don’t care...
0
30
40. Memory Management
alloc retain count I care!
1 2
I care! I don’t care any longer
I care! me neither 1
5
I care! neither do I neither do I
I don’t care... dealloc
0
30
41. The NARC Rule
• Every new, alloc, retain, copy (and
mutableCopy) call MUST be balanced
with a release
• This still holds even under ARC (Automatic
Retain Count). The only difference is that
the rule is automatically respected by the
compiler for you.
31
42. Accessor methods and
memory management
- (void)setName:(NSString *)newName {
if (newName != self->name) {
/* release the old object*/
[self->name release];
/* retain the new one */
self->name = [newName retain];
}
}
32
43. Memory policies
• strong/retain: ‘normal’ reference that
increases the retainCount
• copy: same as retain, but clones the object
• weak: ARC specific. Does not retain the
object. Automatically nullify the pointer
• assign: pre-ARC. Same as weak but dåoes
not automatically nullify the pointer!
33
44. Other options
• nonatomic/atomic: thread-safe or not.
Default is atomic.
• getter/setter: defines the getter/setter
names
• readonly/readwrite: whether produce the
setter or not. Default is readwrite.
34