开发者

Why the usage of instance pointers is different than the usage of number pointers (objective-c, Mac)

Recently I have started learning objective-c on Mac, but I have stumbled upon the basic understanding the usage of the pointers. So far I got the idea about working with a number pointers:

int x = 1; // initializing main variable x
int *y; //  initializing variable y of the int type pointer
y = &x; // passing the reference
*y = *y + 1; // working with a value via passed reference

Lets look through another example, for example with NSString object:

NSString *comp; // initializing a pointer of NSString type. 
comp = @"Mac"; // ???

The problem is, I do not understand why it is OK to use comp = @"Mac" w开发者_StackOverflow社区ithout an asterisk instead of using with *comp = @"Mac"? Is it some kind of a shortcut when dealing with object pointers? It would be more clear (for me:) ) if the same syntax rules would be applied both to simple data type pointers and to object type pointers. I know that almost all object variables are pointers in objective-c, but what is the reason for such syntax?


Objective C objects are not values in the way that your number examples are. The actual data structure is pretty much opaque to the programmer and managed by the runtime. All you deal with is the pointer to it. You never actually directly modify the stuff pointed to, instead you send messages that lead to it being modified. Whereas with your simple pointer to an int you are getting right on in there and changing the content (the value of x) yourself.

The NSString example is a slightly special case and it's not at all unreasonable to find it confusing. The deal is that this bit: @"Mac" instantiates the memory of the string object somewhere, but what it actually gives back for you to assign is only the pointer to that object. So this line:

comp = @"Mac";

is an assignment of a pointer (@"Mac") on the right to a pointer variable (NSString* comp) on the left. On neither side are you really dealing with the value directly, it just sort of looks like you are.

The reason all this works this way, rather than in the perhaps more intuitive fashion of something like Java, is because it is all based on C and uses the same syntax. But the advantage, once you get used to it, is that it should act as a constant reminder that you're only ever dealing with a reference to your objects, and the nitty gritty is managed behind the scenes. (That's a good thing, honest.)


It can be very confusing at first, but much easier to understand once you break down the purpose of each operator.

The ampersand (&) is a reference operator, it simply returns the address in memory of a particular variable.

int a = 5;
// a  == 5        the value
// &a == 0x1000   the address

The asterisk (*) is a dereference operator, a pointers value is an address and using the asterisk causes the value of what it points to be returned.

int a = 5;
int b = &a;
// a  == 5        the value
// &a == 0x1000   the address
// b  == 0x1000   the value of b is the address of a
// *b == 5        the value of b is an address that points to the value of 5

So in the case of...

NSString *aString;
aString = @"this is a string"

What is happening behind the scenes is the address of @"this is a string" is being assigned to the value of aString. When using the string you would not dereference it because that would merely point to the value the first block of memory that object takes up which is useless since you work on the entire object as a whole.


The asterisk is only used in variable declaration to say that the value is a pointer and outside of that it will dereference a pointer as you pointed out in your first example *y = *y + 1

@"" literal is a NSString * (NSString pointer) similar to "" literal being a const char * (constant character pointer).

In your example it is the same thing to do the following.

NSString *comp;
comp = @"Mac";
//^^ is the same as
NSString *comp = @"Mac";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜