objective-c oop spring 2003. oop conceptually the same as c++, java, and all other object oriented...
TRANSCRIPT
Objective-COOPSpring 2003
OOP
Conceptually the same as C++, Java, and all other object oriented languages
The syntax, however…
…is, well, different
Apple’s documentation on Objective-C classes:
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/DefiningClasses/DefiningClasses.html
OOP
Examples in these slides are available as a compressed Xcode project (intro_classes.xcodeproj) at:
http://www.ithaca.edu/barr/Student/CS345/Examples/intro_classes.zip
Methods & MessagesMethods of class instances are called by passing messagesThe syntax is square brackets
The instance is the first item in the backetsThe message or method name is the second item
#import <Foundation/Foundation.h>#import "alien.h"
int main(int argc, const char * argv[]){
@autoreleasepool {
alien *george; double timeToHome; george = [[alien alloc] init]; timeToHome = [george calcTimeToHome];
// continued on next slide
Variable holding object
Method being called (note: no parameters)
All variables that hold objects must be pointers
Brackets around the call
If you try to use a static variable to hold an object will get the error “interface type cannot be statically allocated”
Methods & MessagesThe syntax is square brackets
The instance is the first item in the backetsThe message or method name is the second itemThe argument is the third item
#import <Foundation/Foundation.h>#import "alien.h"
int main(int argc, const char * argv[]){
@autoreleasepool {
alien *george; double timeToHome; double timeToPlace; george = [[alien alloc] init]; timeToPlace =[george timeToPlace: 22000];
// continued on next slide
Variable holding object
Method being called (note: 1 parameter)
Argument
If you try to use a static variable to hold an object will get the error “interface type cannot be statically allocated”
Methods & MessagesThe syntax is square brackets
The instance is the first item in the backetsThe message or method name is the second item followed by a colonThe first argument is the third itemThe next part of the method name is the fourth item followed by colon
#import <Foundation/Foundation.h>#import "alien.h"
int main(int argc, const char * argv[]){
@autoreleasepool {
alien *george; double timeToHome; double timeToPlace; george = [[alien alloc] init]; timeToPlace =[george timeWithSpeed: 1000.4 atDistance: 1230000.5];
// continued on next slide
Variable holding object
Method being called (note: 2 parameters)
First argument
Second argument
PropertiesChanging properties
Setters and getters are created automatically by the @synthesize directiveTwo ways of using setters and getters: brackets and dot-notation
#import <Foundation/Foundation.h>#import "alien.h"
int main(int argc, const char * argv[]){
@autoreleasepool { alien *george; double timeToHome = 0.0; george = [[alien alloc] init]; george.planet = @"Mars"; george.numEyes = 8; george.distanceToHome = 10000; // light years george.speedSpaceShip = 10.4; // hours/light year } return 0;}
Normal bracket notation
Dot notation
It’s good to initialize variables when you declare them. This isn’t necessary for object pointers, because the compiler will automatically set the variable to nil if you don’t specify any other initial value:
Creating Classes
Interface and implementation are separate (like C++)Can put both in same file
Or in separate .h and .m files
Interface (.h file):
#import <Foundation/Foundation.h>
@interface alien : NSObject { //properties double distanceToHome;}// methods- (double) calcTimeToHome;
@end
Even console applications use the Foundation framesworks
The interface starts with @interface
The interface ends with @end
All classes inherit from NSObject Indicate inheritance with colon: NSObject
Methods starting with a dash are instance methodsMethods starting with a plus sign are class methods
Return type
Implementation fileImplementation file (.m file):
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
- (double) calcTimeToHome{ double theTime; theTime = distanceToHome / speedSpaceShip; return theTime;}@end
Must import the .h file
The implementation starts with @implementation nameOfClass
The implementation ends with @end
Use the @synthesize directive to create setters and getters automatically (see later slide)
Signature is the same as in the interface file.
Return value
Method with a parameter
Implementation file (.m file):
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
- (double) timeToPlace: (double) dist{ double theTime = dist / speedSpaceShip; return theTime;}
@end
Each argument must have a selector and a formal parameter
Signature is the same as in the interface file.
Return value
Method with two parameters
Implementation file (.m file):
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
- (double) timeWithSpeed: (double) theSpeed atDistance: (double) theDist{ double theTime = theDist / theSpeed; return theTime;}
@end
Each argument must have a selector and a formal parameter
Signature is the same as in the interface file.Return
value
init methodImplementation file (.m file):
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
- (id) init{ if (self = [super init])
{ numEyes = 4;
planet = @"Neptune"; distanceToHome = 1000000; speedSpaceShip = 1000; return (self);}return nil;
}@end
First call the init of the superclass. It may return a new instance of you and it may fail (in that case it returns nil and the if statement fails)
Signature is the same as in the interface file.
Must return self or nil for george = [[alien alloc] init] to work.
In objective-C self is a built-in variable that allows code to refer to itself.
init method with parameters
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
- (id) initWithNum: (int) a andDistance: (double) b andPlanet: (NSString *) c{ if (self = [super init]) { numEyes = 4;
planet = c; distanceToHome = b; speedSpaceShip = a;
return (self); } return nil; }
@end
Use the @synthesize directive to create setters and getters automatically (see next slide)
Signature is the same as in the interface file.
First call the init of the superclass. It may return a new instance of you and it may fail (in that case it returns nil and the if statement fails)
Must return self or nil for george = [[alien alloc] initWithNum: 4 andDistnce: 22000] to work.
Xcode 4.5: To return “self” the method name must begin with “initWith” where only the ‘W’ is capitalized.
@synthesizeIn Xcode 4.5, the @synthesize statement in the .m file is now optional, and properties are synthesized automatically.
Automatically generated IBOutlet properties therefore no longer add @synthesize because it's no longer required.
When properties are synthesized automatically (without an explicit @synthesize statement) the corresponding instance variable is prepended with an underscore.
This is so the instance variable and property names don't overlap.
Accessing instance variables and properties has not changed. All that has changed is the default name of the instance variable. E.g.:
_foo = @"Bar"; // Setting an instance variable directly.
self.foo = @"Bar"; // Setting an instance variable via a property accessor method.
The underscore is merely a matter of style, so that it's more clear that you're accessing the instance variable rather than the property.
You can add a @synthesize statement yourself, and that will force the name of the corresponding instance variable to be whatever you want it to be.
Likewise, if you add your own property accessor methods then that will prevent the instance variable from being generated automatically.
@synthesize
Example of the use of properties:
[george setPlanet:@"Mars"];
george.numEyes = 8;
george.distanceToHome = 10000; // light years
george.speedSpaceShip = 10.4;
theSpeed = [george speedSpaceShip];
theDistance = george.distanceToHome;
NSLog(@"George's speed is %.2f and his distance to home is %.2f\n", theSpeed, theDistance);
@synthesize automatically gives a setter:[object setVariableName: value]
Where “set” is lowercase, variable name is same as property except first letter is always capitalizedOr: can use the dot notation. Do not change variable name.
@synthesize automatically gives a getter:[object variableName]
Where “variable name is same as Or: can use the dot notation. Do not change variable name.
description methodThe description method. Override the method from the superclass. Do not have to declare in the interface.
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
- (NSString *) description{ NSString *aboutMe; aboutMe = [NSString stringWithFormat:@"I am an alien that lives on %@ with %d eyes!", planet, numEyes ];
return aboutMe;} // description}
@end
This method is not in the .h file. It is called when you print an instance of this class.The method is used to allow a class to print out a string describing itself.
stringWithFormat is a class method. So we don’t use a variable as the first object, but the name of the class. Note that this particular method (stringWithFormat) works like printf instead of like normal methods!
description methodThe description method. Override the method from the superclass. Do not have to declare in the interface.
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
sally = [alien new];
NSLog(@"Here's the description: %@", sally);
@end
you can use new if there are no arguments to the constructor; this does both alloc and init
Arrays of objectsTo put objects in arrays use the id class
#import "alien.h"
@implementation alien
@synthesize numEyes, planet, distanceToHome, speedSpaceShip;
id theAliens[5]; // automatically an array of pointers
// insert code to create two aliens in variables george and sally
theAliens[0] = george; // automatically stores address theAliens[1] = sally;
NSLog(@"Here's george's description: %@", theAliens[0]);
@endFrom Apple’s documentation: As mentioned earlier, you need to use a pointer to keep track of an object in memory. Because of Objective-C’s dynamic nature, it doesn’t matter what specific class type you use for that pointer—the correct method will always be called on the relevant object when you send it a message.
The id type defines a generic object pointer. It’s possible to use id when declaring a variable, but you lose compile-time information about the object.
id is Objective-C’s root object (it’s a NSObject ).
Determining Variable type
To determine the type of a variable holding a class, use class methods from the NSObject class
if ([george isKindOfClass:[alien class]]) NSLog(@"george is an alien");else NSLog(@"george is NOT an alien (despite what you think)");
NSLog(@"george is of type %@", [alien class]);
The class name
The variable that holds an instance of a class.
The class method returns a NSString
Pass-by-referenceCommon technique in many languages
Example:
void f(int &x){
x = 55;}int main(int argc, char*argv[]){ int x = 42; f(y); printf(“y = %d\n”, y); // prints 55}
Does not exist in Objective-C, even if you’re using a pure C function in the .m file!
Virtual methods (not)The term "virtual function" has no direct counterpart in Objective-C.
In Objective-C you don't call functions on objects, you send messages to them.
The object itself then decides how to respond to the message, typically by looking up the message in its class object, finding the associated method and invoking it.
This all happens at run time, not compile time.
The mapping between messages ("selectors" is the technical term) and methods is built entirely from the @implementation.
The method declarations in the @interface are only there to give the compiler the information it needs to warn you that you may have forgotten a method implementation.
And it is only a warning because you can't tell until run time whether whether the object really does respond to the message or not.
For example, somebody else could add a category to an existing class that provides implementations for missing methods, or a class could override forwardingTargetForSelector: to forward messages it doesn't respond to elsewhere.
This is from JeremyP on stackOverflow: http://stackoverflow.com/questions/4374677/what-is-the-equivalent-of-a-c-pure-virtual-function-in-objective-c Not clear this is true for
the most recent version of Objective-C
C++ vs Objective C
Virtual vs ??
Abstract vs protocol
?? Vs categories