1 / 11

Intro to Objective-C Syntax:

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.

kendra
Download Presentation

Intro to Objective-C Syntax:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: voiddoNothing() { // nothing } intaddThree(intx) { return x + 3; } intmultiplyThreeParameters(intx,inty,intz) { 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) xByThis:(int) yAndThis:(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:param1method: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 = [msgcomponentsSeparatedByString:@" "]; // The @ is required for all string literals, and encodes the string using UTF8

  6. Intro to Objective-C Syntax: Instantiation / Memory Allocation In Java: Objecto = 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= [[Objectalloc] init]; OR Object*obj= [Object new]; [objrelease]; OR [objautorelease]; 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 Circle.h @interface Circle : NSObject{ // instance variables doubleradius = 1.0; } // Class methods +(double) getPi; // Instance methods -(double) getArea; -(void) setRadius:(double) r; @end Key class name superclass return type method name parameter type parameter name Optional parameter name

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

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

More Related