Placement of the asterisk in Objective-C

前端 未结 14 1119
鱼传尺愫
鱼传尺愫 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:15

    1, 2 and 4 are equivalent and define a pointer to an NSString. My personal preference is to emulate K&R as much as possible, so I like to use NSString *string;

    (NString*)string; though a valid statement, doesn't really do anything by itself.

    $ cat foo.m
    #include 
    
    void foo()
    {
        NSString *string;
    
        (NSString*) string;  // doesn't do anything
        42;   // doesn't do anything either
    }
    
    $ gcc -Wall -c foo.m
    foo.m: In function 'foo':
    foo.m:7: warning: statement with no effect
    foo.m:8: warning: statement with no effect
    

提交回复
热议问题