Objective C, syntax basics part 1.March 01, 2011 06:50 am Posted By: bytedissident
Whether you are learning a new programming language or Objetive C is the first language you have ever grappled with getting the basic syntax memorized is a necessity.
Anatomy of instantiating an object:
No matter the object the syntax is uniform
ClassName *instanceName = [ClassName alloc];
[instanceName init];
For convenience you will often this done with nested functions like so
ClassName *instanceName = [[ClassName alloc]init];
What does the above code really do?
1. ClassName declares what Class/Object we are going to instantiate
2. *instanceName creates our instance name, a referential name to the class. The * is a pointer that tells the compiler that the next piece of text is the instance name.
3. = is needed in order for us to set a value to the instanceName
4. [ClassName Alloc] , this says create a space in RAM for this object
5. [instanceName init] , this is the final initialization method call. Nothing happens without it.
Calling Methods/Functions:
[instanceName methodName];
All methods are called by enclosing in brackets instance name then a space then method name.
Instances with parameters are called like so:
[instanceName methodName:parameter];
These basic rules apply to all objects/classes whether created by you or if you are using one of Apple’s APIs like UITextView. I hope this helps.
|