110 likes | 254 Vues
This guide introduces Objective-C syntax, focusing on the unique aspects that confuse many developers. Key topics include naming conventions, method signatures, and memory management differences compared to Java and C++. Learn about class names, method declarations, accessing methods of objects, and the specific syntax used for instantiating and releasing objects. With examples illustrating method signatures and classes, this resource aims to clarify the essentials of Objective-C for newcomers and seasoned programmers alike.
E N D
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: 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
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
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: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
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);
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”.
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
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
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; }