5. Inventors
• Objective‐C was invented by two men, Brad
j
y
,
Cox and Tom Love.
• Both were introduced to Smalltalk at ITT in
1981
• Cox thought something like Smalltalk would
be very useful to application developers
be very useful to application developers
• Cox modified a C compiler and by 1983 he had
a working Object oriented extension to C
a working Object‐oriented extension to C
called OOPC.
6. Development
• Tom Love acquired a commercial copy of
Tom Love acquired a commercial copy of
Smalltalk‐80 while working for Schlumberger
Research
• With direct access Smalltalk, Love added more
to OOPC making the final product, Objective‐
to OOPC making the final product Objective
C.
• I 1986 they release Objective‐C through their
In 1986 h
l
Obj i C h
h h i
company “Stepstone”
7. NeXT and NeXTSTEP
NeXT and NeXTSTEP
• In 1988 Steve Jobs acquires Objective‐C
q
j
license for NeXT
• Used Objective‐C to build the NeXTSTEP
Operating System
• Objective‐C made interface design for
NeXTSTEP much easier
much easier
• NeXTSTEP was derived from BSD Unix
• In 1995 NeXT gets full rights to Objective C
In 1995 NeXT gets full rights to Objective‐C
from Stepstone
8. OPENSTEP API
OPENSTEP API
• Developed in 1993 by NeXT and Sun
p
y
• An effort to make NeXTSTEP‐like Objective‐C
implementation available to other platforms.
• In order to be OS independent
– Removed dependency on Mach Kernel
– Made low‐level data into classes
• Paved the way for Mac OS X, GNUstep
9. Apple and Mac OS X
Apple and Mac OS X
• NeXT is taken over by Apple in 1996 and put
y pp
p
Steve Jobs and his Objective‐C libraries to
work
• Redesigned Mac OS to use objective‐C similar
d
d
b
l
to that of NeXTSTEP
• Developed a collection of libraries named
Developed a collection of libraries named
“Cocoa” to aid GUI development
Release Mac OS X (ten), which was radically
• Release Mac OS X (ten) which was radically
different than OS 9, in March 2001
10. The Cocoa API
The Cocoa API
• Primarily the most frequently used frameworks
Primarily the most frequently used frameworks
nowadays.
• Developed by Apple from NeXTSTEP and OPENSTEP
p
y pp
• Has a set of predefined classes and types such as
NSnumber, NSstring, Nsdate, etc.
g
• NS stands for NeXT‐sun
j
,
• Includes a root class NSObject where words like alloc,
retain, and release come from
14. A brief introduction
A brief introduction
– Object model
Object model
– Square brackets
– Naming conventions
Naming conventions
– Importing
– Class definition and implementation
Class definition and implementation
– Exception handling
– Nil objects
Nil objects
– Memory management
15. Object Model
Object Model
• provides messaging‐style syntax that involves
provides messaging style syntax that involves
passing messages to object instances, rather
than calling methods on objects.
than calling methods on objects
16. Square Brackets and Methods
• The object model is based around the concept
The object model is based around the concept
that objects are sent messages in order to
invoke a method.
• The square brackets indicate that you are
The square brackets indicate that you are
sending a message to an object.
• Example:
[ diesel start];
[ diesel start];
17. Methods and Messaging
This declaration is preceded by a minus (-) sign, which indicates
that this is an instance method.
[myArray insertObject:anObject atIndex:0];
20. Naming Conventions
• much like other languages
much like other languages,
– using PascalCase for classes
– and camelCase for methods and properties
and camelCase for methods and properties.
21. Importing
•
Two ways of importing, just as with C/C++.
Two ways of importing just as with C/C++
–
–
•
1. Angle Bracker <>
2. Double Quote “”
The difference is that the syntax of
–
–
force the compiler’s preprocessor to look for the file in the system header directory,
Quotes syntax will look in the current directory if you haven’t specified an alternative location.
•
To look in the system header directory, use the following syntax:
•
#import <Foundation/foundation.h>
•
To look for your own header file in the current or specific directory, use the following syntax:
•
#import "myfile.h”
22. Class Definition and Implementation
Class Definition and Implementation
• As with most object‐oriented languages,
– an object is defined by its class,
– and many instances of that object may be created.
• Every class consists of
– an interface, which defines the structure of the class
– and allows its functionality to be implemented.
• Each class
Each class
– has a corresponding implementation
– that actually provides the functionality.
25. Class in Obj C
Class in Obj‐C
Engine.h
Engine h
@interface Engin
‐ (int) revs;
@end
Engine.m
@implementation Engin
‐ (int) revs (
return revs;
}
‐ (void) start {
// Start the engine at an idle speed of 900 rpm
// – NOTE This is a comment
revs=900;
900
}
@end
26. Nil Objects
Nil Objects
• Methods are
– implemented as messages being passed to objects whose
correct identity is resolved at runtime.
• a mismatch during runtime,
– either an exception will be thrown (best‐case scenario)
– or the object will silently ignore the message (worst‐case
h b
ll l l
h
(
scenario).
• Be extra careful about both
– ensuring messageobject interaction is valid
– and that good exception handling is added throughout
and that good exception handling is added throughout.
28. Memory Management
Memory Management
• A referencecounting system is used by Objective‐C.
•
• This means that
– if you keep track of your references, the runtime will automatically
reclaim any memory used by objects once the reference count returns
l i
d b bj t
th
f
t t
to zero.
• NSString* s = [[NSString alloc] init]; // Ref count is 1
NSString s [[NSString alloc] init]; // Ref count is 1
• [s retain]; // Ref count is 2 ‐ silly
• // to do this after init
• [s release]; // Ref count is back to 1
• [s release]; // Ref count is 0, object is freed
32. Method Declaration
• Two type of Method:
– Class Method
– Instance method
• A class method
– indicated by a plus (+) character.
– associated with the class type.
• An instance method
– indicated by a minus (‐) character
indicated by a minus (‐) character.
– associated with an instance object associated with the
class.
41. Creating Your First iPhone
Creating Your First iPhone Application
•
•
•
•
•
1. Create your project.
1 Create your project
2. Design your application.
3. Write code.
3
i
d
4. Build and run your app.
5. Test, measure, and tune your app.