4. Objective-C – What is it?
Simple extension of the C language
Adds object-oriented capabilities to C language
Runtime system (C library)
Dynamic typing
Dynamic binding
GCC/Clang compiler supports both C and Objective-C
Apple donated Objective-C for GNU project (open source)
www.exoplatform.com - Copyright 2012 eXo Platform 4
5. Objective-C – Who use?
Apple Inc.
Mac OS X developers
Cocoa framework
Core animation, Core data, etc
iOS developers
Cocoa touch framework
Core animation, Core data, etc
www.exoplatform.com - Copyright 2012 eXo Platform 5
6. Objective-C – Why should I (Java developer) care?
Make up your mind with new code designs
iPhone/iPad devices become popular rich user agents in
enterprise market
eXo Platform 3.5, cloud-workspaces.com are integrated with iOS
based devices and Android family
Play around for fun or profit in free time ;)
www.exoplatform.com - Copyright 2012 eXo Platform 6
7. Objective-C – History
Appeared in 1983
Designed by Tom Love & Brad Fox
Major implementations GCC, Clang
Influenced by Smalltalk, C
OS Cross-platform
www.exoplatform.com - Copyright 2012 eXo Platform 7
8. Objective-C and Java
Java
Almost everywhere
… except iPhone
Objective-C
Platform
Mac OS X
iPhone, iPad, ...
www.exoplatform.com - Copyright 2012 eXo Platform 8
10. Objective-C and Java Method arguments
Java
person.setFirstName(“Fred”)
Objective-C
* Arguments are delimited by colons
[person setFirstName:@”Fred”]
www.exoplatform.com - Copyright 2012 eXo Platform 10
11. Objective-C and Java Object Data types
Java
Employee emp = new Employee();
Objective-C
* Objective-C objects are dynamically allocated structs
Employee *emp = [[Employee alloc] init];
* Providing generic object type, id
id emp2 = [[Employee alloc] init];
www.exoplatform.com - Copyright 2012 eXo Platform 11
12. Objective-C and Java Object Data types
Java
* Constructors
Employee emp = new Employee();
Objective-C
Employee *emp = [[Employee alloc] init];
* Creation methods are just methods
* Calls to super can occur anywhere within a method
* Inheritance is straight-forward
* Memory allocations and initialization are separate steps
www.exoplatform.com - Copyright 2012 eXo Platform 12
13. Objective-C and Java Prefix vs Package path
Java
java.lang.String s = new String(“hello”);
Objective-C
* Objective-C doesn't provide namespaces
* Frameworks and libraries use prefixes by convention to avoid collision
NSString *s = [NSString alloc] initWithString:@”Hi”];
==> NS is prefix for classes of Foundation Framework.
www.exoplatform.com - Copyright 2012 eXo Platform 13
14. Objective-C and Java Method prototypes
Java public void sayHello() {
…
}
Objective-C
* Methods declared in .h file, implemented in .m
* Instance methods prefixed with -
* Class methods prefixed with +
Ex:
// Method declarations
- (id)init;
- (id)alloc;
www.exoplatform.com - Copyright 2012 eXo Platform 14
15. Objective-C and Java Method prototypes
Objective-C
* No method overloading
* Runtime system looks up methods by name rather than signatures
* Method names can be composed of multiple sections
Ex:
- (void)addEmployee:(Employee *)emp withTitle:
(NSString *)title
Name of method is addEmployee:withTitle
www.exoplatform.com - Copyright 2012 eXo Platform 15
16. Objective-C and Java Class
Java
...
Objective-C
www.exoplatform.com - Copyright 2012 eXo Platform 16
17. Objective-C and Java Anatomy of Class Declaration
www.exoplatform.com - Copyright 2012 eXo Platform 17
19. Objective-C and Java Class
Java
...
Objective-C
www.exoplatform.com - Copyright 2012 eXo Platform 19
20. Objective-C – Memory management
* Reference counting
– (id)retain; // increase retain count
– (id)release; // decrease retain count
– (id)autorelease; // release with a delay
– (void)dealloc; // call by release when retain
count = 1
* Creation methods set retain count to 1
– Creation methods whose names start with alloc or new or
contain copy
– Those who call creation methods MUST call either release or
autorelease also
– Never call dealloc directly
www.exoplatform.com - Copyright 2012 eXo Platform 20
24. Objective-C – Blocks
A block of code
– A sequence of statements inside {}
– Start with the magical character caret ^
Ex:
[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key,
id value, BOOL *stop) {
NSLog(@“value for key %@ is %@”, key, value);
if ([@“ENOUGH” isEqualToString:key]) {
*stop = YES;
}
}];
www.exoplatform.com - Copyright 2012 eXo Platform 24
25. Objective-C – Blocks
A block of code
– Can use local variables declared before the block inside the
block
– But they are read only!
Ex:
double stopValue = 53.5;
[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key,
id value, BOOL *stop) {
NSLog(@“value for key %@ is %@”, key, value);
if ([@“ENOUGH” isEqualToString:key] || ([value
doubleValue] == stopValue)) {
*stop = YES;
stoppedEarly = YES; // ILLEGAL
}
}];
www.exoplatform.com - Copyright 2012 eXo Platform 25
26. Objective-C – Blocks
When do we use blocks in iOS?
– Enumeration
– View Animations
– Sorting (sort this thing using a block as the comparison method)
– Notification (when something happens, execute this block)
– Error handlers (if an error happens while doing this, execute this
block)
– Completion handlers (when you are done doing this, execute this
block)
And a super-important use: Multithreading
www.exoplatform.com - Copyright 2012 eXo Platform 26
28. Xcode
IDE for iPhone projects
– Build
– Run (Simulator, device)
– Debug
– Source code management (SCM)
– Documentation
www.exoplatform.com - Copyright 2012 eXo Platform 28
29. Xcode
Automatically maintain build scripts
Display logical grouping of files
No package paths
By default, groups not mapped to folder structure
Resources
Automatically bundled with executable
Frameworks
Linked at compile time; no classpath needed
www.exoplatform.com - Copyright 2012 eXo Platform 29
30. Xcode - Interface Builder
Visual GUI design tool
Doesn't generate code
Working with “freeze-dried” objects
– Archived (serialized) in .nib files
– Dynamically loaded
– Objects deserialized at load time
www.exoplatform.com - Copyright 2012 eXo Platform 30
36. iOS – MVC Pattern
Model
– Manages the app data and state
– No concerned with UI or presentation
– Often persists somewhere
– Same model should be reusable, unchanged in different
interfaces.
www.exoplatform.com - Copyright 2012 eXo Platform 36
37. iOS – MVC Pattern
View
– Present the Model to the user in an appropriate interface
– Allows user to manipulate data
– Does not store any data
– Easily reusable & configurable to display different data
www.exoplatform.com - Copyright 2012 eXo Platform 37
38. iOS – MVC Pattern
Controller
– Intermediary between Model & View
– Updates the view when the model changes
– Updates the model when the user manipulates the view
– Typically where the app logic lives.
www.exoplatform.com - Copyright 2012 eXo Platform 38
39. iOS – Delegation Pattern
Control passed to delegate objects to perform application specific
behavior
Avoids need to subclass complex objects
Many UIKit classes use delegates
– UIApplication
– UITableView
– UITextField
www.exoplatform.com - Copyright 2012 eXo Platform 39
40. iOS – Target/Action Pattern
When event occurs, action is invoked on target
object
Target: myObject
sayHello Action: @selector(sayHello)
Event: UIControlEventTouchUpInside
Controller
www.exoplatform.com - Copyright 2012 eXo Platform 40
41. References
Training for newcomer of Mobile team -
http://int.exoplatform.org/portal/g/:spaces:mobile/mobile/local._wiki.WikiPortl
et/group/spaces/mobile/iOS_Training
Jonathan Lehr -
http://jonathanlehr.files.wordpress.com/2009/09/objective-c-and-java.pdf
stanford.edu - http://www.stanford.edu/class/cs193p/cgi-bin/drupal/
www.exoplatform.com - Copyright 2012 eXo Platform 41
Objective-C in brief Xcode MVC, Delegate and action-target patterns Cocoa Touch Frameworks Blocks, multithreading, categories
* Objective-C is a superset of C Objective-C, being a C derivative, inherits all of C's features. There are a few exceptions but they don't really deviate from what C offers as a language. * Likewise, the language can be implemented on top of existing C compilers (in GCC, first as a preprocessor, then as a module) rather than as a new compiler. This allows Objective-C to leverage the huge existing collection of C code, libraries, tools, etc. Existing C libraries can be wrapped in Objective-C wrappers to provide an OO-style interface. In this aspect, it is similar to GObject library and Vala language, which are widely used in development of GTK applications. * Starting in 2005, Apple has made extensive use of LLVM in a number of commercial systems,[4] including the iPhone development kit and Xcode 3.1.
* Today, objective-C is used primarily on Apple's Mac OS X and iOS * It is the primary language used for Apple's Cocoa API *
* Objective-C includes many concept and pattern which are useful for developers to know. *
Watch a video illustrate how to create an iOS application by Xcode
Watch a video illustrate how to create an iOS application by Xcode