Cocoa Objects

Coco Objects are actually pretty easy when you think about it. Every object has properties and or methods that can be called. When instantiating a cocoa object you have to allocate it and then initialize it. You can do that in a few ways but the most common way is to use a single line. Below is showing how you can instanciate an NSNumber Object.

OBJC:
  1. // Method #1 - Prefered Method
  2. NSNumber *number = [[NSNumber alloc] init];
  3.  
  4. // Method #2
  5. NSNumber *number = [NSNumber alloc];
  6. [number init];
  7.  
  8. // Method #3
  9. NSNumber *number;
  10. [number alloc];
  11. [number init];

In the code above you can see why method #1 is the preferred way, it is the easiest to read. Basically when instantiating an object in Objective-C you are saying I am creating a variable called number which is going to hold a value of type NSNumber. In the code to the right of the equals sign you are allocating and initializing the actual object. Pretty easy huh?


Technology Blogs Add to Technorati Favorites Page Rank Tool NYPHP Users Group View Joseph Crawford's profile on LinkedIn

   

SEO Consultant SEO services