What's the difference?
I'll preface my question by saying that I'm a beginner objective-c developer. What is the difference between:
NSString * foo;
NSStri开发者_JAVA百科ng* foo;
NSString *foo;
Is there a difference?
All three simply declare a variable named foo of type NSString *. It is really just a matter of style preference.
Some people prefer to put the asterisk next to the type to emphasize that this is a pointer type.
Some people prefer to put the asterisk next to the variable to emphasize the requirements of the language. Every pointer variable in a multiple declaration needs to have the asterisk, as in:
NSString *foo, *bar;
My personal preference is actually the first of your examples with a space before and after the asterisk, reserving the use of an asterisk directly before the variable for use in dereferencing the pointer. I also avoid declaring more than one variable in a single declaration.
There is no difference, they mean all three the same thing. Only it is considered to be better to declare it like:
NSString *foo;
Because when you add a second variable your intention is more clear:
NSString *foo,*bar;
No difference. The three work the same.
The difference is that you have the space in a different location in each example :-)
Semantically, there is no difference whatsoever. Stylistically, the last form is preferred.
精彩评论