[Aaron Hillegass] Objective-C Programming the Big(BookFi.org)
Objective-C - USALmaxus.fis.usal.es/HOTHOUSE/berlin/2010/hu2010objc.pdf · Stephen G. Kochan...
Transcript of Objective-C - USALmaxus.fis.usal.es/HOTHOUSE/berlin/2010/hu2010objc.pdf · Stephen G. Kochan...
Objective-Can excursus
Universidad de Salamanca
Humboldt Universität BerlinMai 2010Dr. J.R. García-Bermejo Giner
miércoles 12 de mayo de 2010
First of all...
Danke schön!
miércoles 12 de mayo de 2010
Salamanca as an Erasmus Destination
Just three short videos
miércoles 12 de mayo de 2010
Bio & paraphernalia
Dr. José Rafael García-Bermejo Giner
About the University of Salamanca
http://maxus.fis.usal.es/
COURSE MATERIALS - Please download these files:
http://maxus.fis.usal.es/berlin/2010/hu2010objc.pdf http://maxus.fis.usal.es/berlin/2010/programs.zip
miércoles 12 de mayo de 2010
Contents
Contents========
A little history
The Objective-C Programming Language
Objective-C Applications
miércoles 12 de mayo de 2010
A bit of History
Objective-C===========
Bradford Cox and the Software ICs
The NeXT Story
And then Cocoa
miércoles 12 de mayo de 2010
B. Cox and Software ICs
Object-Oriented ProgrammingAn Evolutionary Approach19861991
Two main ideas
Software ICsA software-component shopping basket
miércoles 12 de mayo de 2010
The NeXT Story
Initially a hardware company BSD, Mach Kernel
NextStep and Objective-C (1990-1993)The first web server (CERN)
An interesting spin-off: WebObjects(AppleStore, iTunes Store!!)
miércoles 12 de mayo de 2010
And then Cocoa
Copland (1987)Gershwin (1994-1996)1996: Apple buys NeXTMac OS X - 2001
miércoles 12 de mayo de 2010
And then Cocoa
iPodiPod TouchiPhoneiPad - May 28th
miércoles 12 de mayo de 2010
And then Cocoa
iPodiPod TouchiPhoneiPad - May 28th
ProgrammableDevices
miércoles 12 de mayo de 2010
And then Cocoa
iPodiPod TouchiPhoneiPad - May 28th
ProgrammableDevices
miércoles 12 de mayo de 2010
...end of Story
Today’s Objective-C
Mac OS X (Objective-C 2.0)
iPhone OS 4.0 (Multitasking at last!)
miércoles 12 de mayo de 2010
Objective-Can excursus (II)
Universidad de Salamanca
Humboldt Universität BerlinMai 2010Dr. J.R. García-Bermejo Giner
miércoles 12 de mayo de 2010
Objective-C, a programming language
Bibliography - a starting point, there is more!
Stephen G. KochanProgramming in Objective-C 2.0 (2nd edition, 2009)Addison-Wesley
Aaron HillegassCocoa Programming for Mac OS X (2008)Addison Wesley
Sengan Baring-GouldTeach yourself Cocoa Touch Programming in 24 hoursSams
miércoles 12 de mayo de 2010
Objective-C, a programming language
And now a bit of hands-on: Viele Grüße!
A program written and compiled from the Terminal
A program written and compiled from XCode
miércoles 12 de mayo de 2010
Objective-C, a programming languageA question of syntax (practice will make things clearer)
C syntax is perfectly valid - but no functions outside classes!
You can call functions from methods (frequent).You can call methods from functions (valid, less common).
Instead of the dot notation, we use the square-bracket notation.
Java Objective-Cdescriptor.method(argument); [pointer method:argument];
Methods calls can be chained
Java Objective-Cframe.getContentPane().add(x); [[obj getName] stringWithCoding:UTF8];
It is frequent to see text-based programs written in C or C++ being given a user interface by means of Objective-C.
miércoles 12 de mayo de 2010
Objective-C, a programming language
Where is the documentation?
Of course, there are man pages.
And, XCode does have much to say.
miércoles 12 de mayo de 2010
Objective-C, a programming language
Messages and nilThis is one important difference from C: the nil pointer is one more pointer. Messages sent to it just do nothing, return 0 and do not produce catastrophic failure of the program.
Example: MessageToNilXCode
miércoles 12 de mayo de 2010
Objective-C, a programming language
Having Class(es)
The definition of a class is normally split into two files:
a .h or interface file a .m or implementation file.
The interface file contains
a list of instance variables (no class variables) a list of class methods (similar to static methods in Java) a list of instance methods
The implementation file contains
the #import of the interface file a list of class methods (similar to static methods in Java) a list of instance methods
miércoles 12 de mayo de 2010
Objective-C, a programming language
Having Class(es)
Interface files look like this:
@interface NameOfClass : NameOfSuperclass <NameOfProtocol_01,...> {
// Instance variables (there are no class variables!)-(type_1) instanceVariable_1;...-(type_M) instanceVariable_N;
}
// Class and Instance methods+(type) classMethod;...-(type) instanceMethod[:(type)argument label:(type)anotherArgument...];
@end
miércoles 12 de mayo de 2010
Objective-C, a programming language
Having Class(es)
Implementation files look like this:
#import “NameOfClass.h”@implementation NameOfClass {
// Class methods, if any+(type) classMethod {...}...// Instance methods, if any-(type) instanceMethod[:(type)argument label:(type)anotherArgument...]{...}
...
@end
miércoles 12 de mayo de 2010
Objective-C, a programming language
Having Class(es)
And quickly, an example and our first challenge.
Write a program that shows how to build classes with attributes, methods etc.
Example: HavingClasses
Can you add an Ellipse class? It should have four attributes:xPositionyPositionsemiMajorAxissemiMinorAxis
and the same methods as Circle, Rectangle or Triangle
miércoles 12 de mayo de 2010
Objective-C, a programming language
A word on initialization - writing good init methods
The perfect init:
-(id)init {self = [super init];if (self != nil) {
// do initializations}return (self);
}
Write a program that shows good programming practices as far as initialisation is concerned. For instance, write a random number generator with a couple of constructors, and test both of them.
Example: GoodStart: equal or different
miércoles 12 de mayo de 2010
Objective-C, a programming language
A word on initialization - writing good init methods
The perfect init:
-(id)init {self = [super init];if (self != nil) {
// do initializations}return (self);
}
Write a program that shows good programming practices as far as initialisation is concerned. For instance, write a random number generator with a couple of constructors, and test both of them.
Example: GoodStart: equal or different
Four main points:Call the superclass initSet selfcheck selfreturn self
miércoles 12 de mayo de 2010
Objective-C, a programming languageBeing Dynamic
Types can be resolved at compile time, and also at runtime
id - An important addition to C. It is a pointer to any type of class, without loosing the class!
Why does it work? Because Objective-C classes inherit a variable called isa, of type Class, which contains the Class of the object. Hence it is possible to access methods,and also the actual name of the class to which the object belongs.
Write a program that actually shows how an id variable does not loose class information. For instance, create two classes with methods to set and get their status, and test them by assigning their pointers to an id variable.
miércoles 12 de mayo de 2010
Objective-C, a programming language
Polymorphism
One of the characteristics of object-oriented languages: the capability to distinguish methods by examining their signatures.
Write a class with polimorphyc methods that do different things. For instance, have the class say something just once or a number of times.
Example: PolymorphXCode
miércoles 12 de mayo de 2010
Objective-C, a programming language
Having Properties
Declaring the right access methods opens the door to Key-Value Coding.Being Dynamic showed the way to find out a class’ Class (and name).This will actually let us access variables of that class. Properties can have a few types of accessor methods:
•readonly•readwrite•copy•assign•retain•getter=name;•setter=name;•nonatomic
Write a class that has properties, define them be means of @property and @synthesize, and test the various ways to access status.
miércoles 12 de mayo de 2010
Objective-C, a programming language
Within scope
Accessibility is regulated in a way similar to Java, by means of directives like @private, @protected, @public and @package.
Example: WithinScope
Write a program that shows how the public, protected and public attributes of a class (Student) can o cannot be accessed from another class (MrSnoop).
miércoles 12 de mayo de 2010
Objective-C, a programming language
Gnothi seauthon, or, know thyself: key-value coding
Nota bene. There is much, much more to this. Check documentation.
Example: KnowThySelf
Write a program that accesses the attributes of a class both by means of access methods and by means of key-value coding. The program must have at least two data clases (Me and You for instance), plus another class (Know) that tests access.
miércoles 12 de mayo de 2010
Objective-C, a programming language
Following Protocol(s)
This is the idea behind Java interfaces. Equivalent syntax is as follows:
public class JavaClass extends ItsSuperclass implements An_Interface [,...] {...}
@interface Objective_C_class : ItsSuperclass <A_protocol [,...]>@required...@optional...@end
Define a protocol and then write a class that implements it. For instance, write three classes and have them implement the same protocol. Of course,the methods in each class must adapt to the class.
Example: AccordingToProtocol
Can you add an Ellipse class that conforms to the protocol?
miércoles 12 de mayo de 2010
Objective-C, a programming language
The benefits of inheritance
•There is just single inheritance, not multiple inheritance.
•Methods and attributes are inherited as expected.
•The super keyword permits us to use overridden methods.
•Object references NEVER loose their identity. They are compatible with other elements within the inheritance hierarchy, but they are still objects of a given class.
Write four classes that show how inheritance (and scope) works. For instance, create an inheritance hieranchy (Grandfather, Father, Sohn) and an external class (BrotherInLaw) and check access to attributes and methods.
Example: ScopesAndInheritance
miércoles 12 de mayo de 2010
Objective-C, a programming language
Something new: Categories and Extensions
Categories are a mechanism that allows us to add new class or instance methods (but no new attributes) to the definition of a class.
Like classes, categories consist of an @interface part (in a .h file) and an @implementation part (in a .m file). The name of the category is the same as the name of the class plus an identifier for the category between parentheses. The name of the file is the name of the class, plus a “+” sign, plus the name of the category and the extension.
File: NameOfClass+NameOfCategory.h”
#import “NameOfClass.h”
@interface NameOfClass(NameOfCategory)
+(type)method:(type)argument......-(type)anotherMethod:(type)argument...
@end
miércoles 12 de mayo de 2010
Objective-C, a programming languageSomething new: Categories and Extensions
And quickly, an example. We are going to add a mechanism for saving and loading to a previous exercise.
A few caveats
No “{“ or “{“ in category definition!Extensions are inherited :)No new attributesAnonymous categories are possible (extensions)Watch naming rules!
Write a program that creates several classes and adds a category to them. The program that check the classes’ access to the methods added by the category.
Example: ShapesWithDiskAccess
Can you add a category to Shape and write the same example?
miércoles 12 de mayo de 2010
Objective-C, a programming languageException handling
Quite similar to Java; it makes use of the directives @try, @catch and @finally. There is also a @throw directive, with the usual sense.
One can throw any object, not just a derivation of NSException. However, most older frameworks will only catch derivations of NSException, hence it is a good idea to throw that kind of exception if you suspect it might have to be caught outside your code.
Write a program that catches and throws exceptions.
Example: ExceptionalBehaviour
miércoles 12 de mayo de 2010
Objective-C, a programming languageIn summary
Objective-C is an object-oriented language with some common points with Java (of which it is the predecessor).
There exists a single-inheritance mechanism (but not multiple inheritance).
There is a category-based extension mechanism.
There is a protocol-based design mechanism (interfaces).
There is an automatic mechanism for access-method creation.
There is a garbage-collection mechanism (and a manually-controlled mechanism too).
It is possible to mix method calls with C-function calls. Within reason, there is compatibility with C++.
miércoles 12 de mayo de 2010
Objective-Can excursus (III)
Universidad de Salamanca
Humboldt Universität BerlinMai 2010Dr. J.R. García-Bermejo Giner
miércoles 12 de mayo de 2010
Objective-C Applications
The Tools: XCode and Interface Builder
iPhone OS and Cocoa Touch
miércoles 12 de mayo de 2010
Objective-C Applications
The Tools: XCode and Interface Builder (DEMO)
See the way an example is built, for instance TextEdit. For 1st pass, just open
miércoles 12 de mayo de 2010
Objective-C Applications
Interface Builder for Cocoa Touch (DEMO)
See the way an example is built, just show the emulator running. For 1st pass, just open XCode for a Cocoa app, then IB
miércoles 12 de mayo de 2010
Objective-C Applications
In conclusion ...
Objective-C is an object-oriented language, somewhat different from Java and C++
It has an extensive set of supporting classes (Frameworks)
Can be used to program Macintoshes
Can be used to program mobile devices (iPod Touch, iPod, iPad)
Tell this backwards: first describe then define
miércoles 12 de mayo de 2010
Objective-Can excursus
Universidad de Salamanca
Humboldt Universität BerlinMai 2010Dr. J.R. García-Bermejo Giner
miércoles 12 de mayo de 2010
Objective-Can excursus
Universidad de Salamanca
Humboldt Universität BerlinMai 2010Dr. J.R. García-Bermejo Giner
The end... of the beginning!
miércoles 12 de mayo de 2010
Objective-Can excursus
Universidad de Salamanca
Humboldt Universität BerlinMai 2010Dr. J.R. García-Bermejo Giner
miércoles 12 de mayo de 2010