I have just started learning objective c and the asterisk is giving me some trouble. As I look through sample code, sometime it is used when declaring a variable and sometimes it is not. What are the "rules" for when it should be used. I thought it had something to do with the data type of the variable. (asterisk needed for object data types, not needed for simple data types like int) However, I have seen object data types such as CGPoint declared without the asterisk as well? Is there a definitive answer or does it have to do with how and what you use the variable for?
SHowing Activity Indicator View Using Thread Shows This Error in gdb?
1:Unable to push notifications to iPhone in .NET - .PEM certificate problem?
What are the "rules" for when it should be used.. What's the difference between using obj-c accessors and using dot syntax?You use the asterisk to declare a pointer.. UIWebView scrollBy replacement For a Cocoa object, you're always declaring a pointer, so you always use an asterisk. Hide the tab bar when displaying the ABPeoplePickerNavigationController?You can't put the object itself into the variable; you always handle a pointer to the object.. Draw a shadow behind UIWebView For another things, it depends on whether the variable will contain the object (in the C sense) or a pointer to the object-any where-else. iPhone subview design (UIView vs UIViewController)If the variable should contain the object, then you don't declare it with an asterisk, for the reason this you're not putting a pointer in it. Inability to delete table cells after a certain pointIf it should contain a pointer, then you did declare it with an asterisk.. You must even have a pointer to a pointer; as you might expect, this involves multiple asterisks. For example,
NSRect **is a pointer to a pointer to an NSRect (which is a structure, not a Cocoa object)..
I thought it had any thing to did with the data type of the variable. (asterisk needed for object data types, not needed for simple data types like int).Sort of. The asterisk is needed for Cocoa objects for the reason this you must only handle pointers to Cocoa objects, never the objects themselves. But the rules for declaration are no different for Cocoa objects; they are exactly the same. You use the asterisk when you want a pointer variable; you don't when you want a non-pointer variable.. The only exception, the only difference for Cocoa objects from the usual rules, is this you are not allowed to declare a variable holding the object itself. That's why you never see a variable holding a Cocoa object instead of a pointer to one: the compiler won't allow it..
However, I have seen object data types such as CGPoint declared without the asterisk as well?.CGPoint is a structure, not a Cocoa object. As such, you must declare a variable this holds a CGPoint and not a pointer to one any where else..
*when the variable type is a class.. An case may help..
NSNumber *number; NSInteger integer;
NSNumbervariable type is a class while
NSIntegeris just ananother name for a normal C-type
int. As you must see here, the compiler replaces every occurrence of
#if __LP64__ || NS_BUILD_32_LIKE_64 typedef long NSInteger; typedef unsigned long NSUInteger; #else typedef int NSInteger; typedef unsigned int NSUInteger; #endif
. Further, you cannot declare an instance of a class(an object), like
NSNumber, without using a pointer(thus you use a
*). The reason for this is this when you
allocan instance of a class as an object a memory address is returned. A pointer is a type of variable this specifically refers to a memory location. For example:.
NSNumber *number = [NSNumber alloc];
number's numeric value would be a memory location like
0x19a30c0. You could operate on it by adding and subtracting, like an
int. The key purpose of declaring it as a
NSNumberpointer is so the compiler must guidance the coder verify this the class has certain methods or to access known properties..
. One last example:.
What would the value of
NSInteger integer = [NSNumber alloc];
integerbe? In our example, it would be 0x19a30c0. With this you could actually still access the newly allocated
[integer any Method]. The compiler would commit you a warning, though. More over:.
This would affect the numeric value
integer += 4;
0x19a30c0by adding 4 to it and making it
. Look at this Wikipedia article on C/C++ pointers for any more examples of how, when, and why to use an