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.
When defining class methods what is the difference between (assuming there in a .h file):
- prepareForSegue is not called after performSegue:withIdentifier: with popover style
- How to programmatically add calendar subscriptions on iOS?
- What is the correct way to proceed with this bool?
- Where should I initialize variables in objective c?
- Check for Internet connection using the iOS SDK
- The best way to remove duplicate values from NSMutableArray in Objective-C?
– (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.
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?
- iPhone URL encoding Problem
- Install gcc on mac osx without installing xcode
- How to I add simulator for iphone 4/5 in Xcode 6.3.1
- Notification not coming via Firebase console
- Convert Seconds Integer To HH:MM, iPhone
- how to add time in current time
- List all subclasses of one class
- iOS AutoLayout multi-line UILabel
- Release ad-hoc ed expired provisioning
- Linker error with libpng under MacOSX
- how to get a accurate timer in ios
- How to avoid “wants to open” dialog triggered by openURL in iOS 9.0
- What is the equivalent of UIApplication.sharedApplication().delegate in WatchKit?
- Can't auth to Facebook with iOS SDK and native Facebook application
- Identifying the country code using mobile carrier in iPhone Programatically?