Download presentation
Presentation is loading. Please wait.
Published byTamsin Scott Modified over 9 years ago
1
Objective-C OOP Spring 2003
2
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/Progr ammingWithObjectiveC/DefiningClasses/DefiningClasses.html http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Progr ammingWithObjectiveC/DefiningClasses/DefiningClasses.html
3
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
4
Methods & Messages Methods of class instances are called by passing messages The syntax is square brackets The instance is the first item in the backets The message or method name is the second item #import #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”
5
Methods & Messages The syntax is square brackets The instance is the first item in the backets The message or method name is the second item The argument is the third item #import #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”
6
Methods & Messages The syntax is square brackets The instance is the first item in the backets The message or method name is the second item followed by a colon The first argument is the third item The next part of the method name is the fourth item followed by colon #import #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
7
Properties Changing properties Setters and getters are created automatically by the @synthesize directive Two ways of using setters and getters: brackets and dot- notation #import #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:
8
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 @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 methods Methods starting with a plus sign are class methods Return type
9
Implementation file Implementation 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
10
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
11
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
12
init method Implementation 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.
13
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.
14
@synthesize In 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.
15
@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 capitalized Or: 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.
16
description method The 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!
17
description method The 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
18
Arrays of objects To 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]); @end From 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 ).
19
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 nameThe variable that holds an instance of a class. The class method returns a NSString
20
Pass-by-reference Common 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!
21
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
22
C++ vs Objective C Virtual vs ?? Abstract vs protocol ?? Vs categories
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.