Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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; } // note methods with multiple parameters are given in a parameter list // that is delimited by commas. Key return type method name parameter type parameter name

3 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; } // 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: Key return type method name parameter type parameter name

4 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);

5 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

6 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);

7 Intro to Objective-C Syntax: Instantiation / Memory Allocation In Objective-C: Object *obj = [[Object alloc] init]; OR Object *obj = [Object new]; [obj release]; OR[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”.

8 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. Circle.h Circle.m include

9 Intro to Objective-C Syntax: Classes @interface Circle : NSObject { // instance variables double radius = 1.0; } // Class methods +(double) getPi; // Instance methods -(double) getArea; -(void) setRadius:(double) r; @end Circle.h Key class name superclass return type method name parameter type parameter name Optional parameter name

10 #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 Circle.m

11 #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; } Main.m


Download ppt "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."

Similar presentations


Ads by Google