Placement of the asterisk in Objective-C

前端 未结 14 1114
鱼传尺愫
鱼传尺愫 2020-11-27 13:34

I have just begun learning Objective-C, coming from a VB .Net and C# .Net background. I understand pointer usage, but in Objective-C examples I see the asterisk placed in s

14条回答
  •  鱼传尺愫
    2020-11-27 14:21

    1.  NSString *string;
    2.  NSString * string;
    3.  (NSString *) string;
    4.  NSString* string;
    

    1, 2 and 4 are exactly identical. It's all style. Pick whatever you want, or mix it up.

    Choice #3 has another meaning also, it's used in casting. For example:

    t = (NSString *)string ;
    

    will cast string to an NSString pointer.

    But choice #3 is the syntax you'd probably use in a .h file or in the function definition in a .m file. Inside an actual function, in code which is "run" it has a different meaning.

提交回复
热议问题