Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

iphone - When should you NOT use the asterisk (*) when declaring a variable in Objective C

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What are the "rules" for when it should be used.

You use the asterisk to declare a pointer.

For a Cocoa object, you're always declaring a pointer, so you always use an asterisk. You can't put the object itself into the variable; you always handle a pointer to the object.

For other things, it depends on whether the variable will contain the object (in the C sense) or a pointer to the object-somewhere-else. If the variable should contain the object, then you don't declare it with an asterisk, because you're not putting a pointer in it. If it should contain a pointer, then you do declare it with an asterisk.

You can 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 something to do 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 because you can 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 that 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 can declare a variable that holds a CGPoint and not a pointer to one somewhere else.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...