Objective-C Clarification; -/+ and *var

I’m teaching myself Objective-C from a book (Cocoa programming for mac OS X) and am about halfway through however I have two questions that aren’t answered or defined in the book.

  1. When defining class methods what is the difference between (assuming there in a .h file):

  2. prepareForSegue is not called after performSegue:withIdentifier: with popover style
  3. How to programmatically add calendar subscriptions on iOS?
  4. What is the correct way to proceed with this bool?
  5. Where should I initialize variables in objective c?
  6. Check for Internet connection using the iOS SDK
  7. The best way to remove duplicate values from NSMutableArray in Objective-C?
  8. – (int) population;

    + (int) population;

The way I see it at the moment is that - methods require the class to be allocated and initialized first however + can be called statically without requiring allocation and initialization. E.g. (in a function in another class)

// Using -
Earth *world = [[Earth alloc] init];
int population = [world population];

// Using +
int population = [Earth population];

If that is correct, when should I use static methods and are they’re any disadvantages with doing so.

  1. When defining a var in either a function paramater or as an actual var in a function, does the use of * mean the var will be an object? e.g. (again in a header file.)

    – (void) setPopulation: (NSNumber *) population; //Use of * as population is of NSNumber

    – (void) setPopulation: (int) population; // population isn’t a class so doesn’t need *

Sorry if any of my terms don’t make sense in the land of Objective-C such as static methods, etc. I’m a PHP and Ruby Programmer.

3 Solutions Collect From Internet About “Objective-C Clarification; -/+ and *var”

The -/+ in method declarations for Objective-C simply denote whether the method is a class method or an instance method. For example, with Objective-C, you cannot send an instance a message that was declared as a class method. For example:

@interface MyObject : NSObject
-(void)myInstanceMethod;
+(void)myClassMethod;
@end

// ...

MyObject* obj = [[MyObject alloc] init];

[obj myInstanceMethod];      // this is okay
[obj myClassMethod];         // this will fail
[[obj class] myClassMethod]; // this is okay
[MyObject myClassMethod];    // this is okay
[MyObject myInstanceMethod]; // this will fail

As to the second part of your question, Objective-C is a strict super-set of C. It adds classes but they are really C data structures whose implementations are hidden from you by the Objective-C runtime. Because of this, classes are always represented as pointers. In C, the * means that the variable is being declared as a pointer to some memory address. You can use pointers with primitive types in C as well, but Objective-C objects must always be referred to by pointers.

There are many great tutorials/introductions to pointers out there. I would suggest simply googling for C tutorial and pointers to learn more.

The + declaration is a class method, you need no instance to call it. Constructors/factory methods need to be class methods. The – declared an instance method, operation on a single instance. Each instance has its own independent state (member variables). This is a fundamental difference in OO programming! In general make most methods instance methods, except for utility classes.

You can see some discussion of when to use static methods in When should I write Static Methods?