Intro to Objective-C Syntax: Method Signatures

advertisement
Intro to Objective-C Syntax:
What’s most confusing about Objective-C?
• Most class names start with NS: NSString, NSObject
• Parameter lists are not comma delimited and method names
are interrupted by parameter names and types.
• There are too many brackets and colons. :[
• Memory Management.
• All these @ symbols confuse me.
• Both C and Objective-C methods are allowed? Weird.
Intro to Objective-C Syntax:
Method Signatures
In Java or C:
void doNothing() {
// nothing
}
int addThree(int x) {
return x + 3;
}
int multiplyThreeParameters(int x, int y, int z) {
return x * y * z;
}
Key
return type
method name
parameter type
parameter name
// note methods with multiple parameters are given in a parameter list
// that is delimited by commas.
Intro to Objective-C Syntax:
Method Signatures
In Objective-C:
- (void) doNothing {
// nothing
}
- (int) addThree:(int) x {
return x + 3;
}
- (int) multiplyThis:(int) x ByThis:(int) y AndThis:(int) z {
return x * y * z;
}
Key
return type
method name
parameter type
parameter name
// note methods of Objective-C classes with multiple parameters have a space to
// delimit the end of the parameter name and the continuation of the method
// name. The actually method name is multiplyThis:ByThis:AndThis:
Intro to Objective-C Syntax:
Accessing methods of objects
In Java: object.method(param1, param2);
In C++: object->method(param1, param2);
In C: (no objects) method(param1, param2);
Intro to Objective-C Syntax:
Accessing methods of objects
In Objective-C: [object method:param1 method:param2];
Example:
If you have a string:
NSString *msg = @"ALL YOUR BASES BELONG TO US";
And you want to split the sentence into an array of words:
NSArray *words = [msg componentsSeparatedByString: @" "];
// The @ is required for all string literals, and encodes the string using UTF8
Intro to Objective-C Syntax:
Instantiation / Memory Allocation
In Java:
Object o = new Object();
// Java takes care of garbage collection. In this statement, memory
// is automatically allocated for the new object. Memory is also
// automatically released when the object is no longer in use.
In C:
Object *o = (Object *) malloc(sizeof(Object));
free (o);
In C++:
Object *o = new Object;
delete (o);
Intro to Objective-C Syntax:
Instantiation / Memory Allocation
In Objective-C:
Object *obj = [[Object alloc] init];
[obj release];
OR
OR
Object *obj = [Object new];
[obj autorelease];
Golden Rule of Objective-C memory management:
When ever you own an object, you must relinquish ownership.
I.E. Whenever you call “retain”, “alloc”, “new”, “copy”, or “mutableCopy”, it
must be paired with a call to “release” or “autorelease”.
Intro to Objective-C Syntax:
Classes
• In Java, students can define and implement a class in a single .java file.
• In C++, students define a class and methods in a .h header file and
implement the methods in a .c file.
• In Objective-C, students define a class and its methods in a .h header file
and implement the methods in a .m file.
include
Circle.h
Circle.m
Intro to Objective-C Syntax:
Classes
Circle.h
@interface Circle : NSObject {
// instance variables
double radius = 1.0;
}
// Class methods
+(double) getPi;
Key
class name
superclass
return type
method name
parameter type
parameter name
// Instance methods
-(double) getArea;
-(void) setRadius:(double) r;
@end
Optional
parameter name
Circle.m
#import "Circle.h"
@implementation Circle
+(double) getPi {
return 3.14159265;
}
-(double) getArea {
double pi = [Circle getPi];
return pi * radius * radius;
}
-(void) setRadius:(double) r {
radius = r;
}
@end
Main.m
#import "Circle.h"
// Non Objective-C function; program origin
int main() {
Circle *mycirc = [[Circle alloc] init];
[mycirc setRadius:3.0];
double area = [mycirc getArea];
double pi = [Circle getPi];
return 0;
}
Download