Placement of the asterisk in Objective-C

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

    There may not be any differences in the alternatives 1, 2 and 4 to the computer but there are to other readers of the code.

    Since explanations like https://stackoverflow.com/a/1521382 and https://stackoverflow.com/a/16040884 and https://www.tutorialspoint.com/objective_c/objective_c_pointers.htm use the first alternative:

    1. NSString *string;

    and all extra variables need their own asterisk like in:

    NSString *aString, *bString;

    my humble suggestion is that we use alternative 1 as a standard.

    0 讨论(0)
  • 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 <Cocoa/Cocoa.h>
    
    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
    
    0 讨论(0)
  • 2020-11-27 14:16

    in the xcode4 documentation sample code you can see 3. all the time, for example in MoveMeView.m

    #if 1
    
    - (void)growAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    #define MOVE_ANIMATION_DURATION_SECONDS 0.15
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS];
    placardView.transform = CGAffineTransformMakeScale(1.1f, 1.1f); 
    /*
     Move the placardView to under the touch.
     We passed the location wrapped in an NSValue as the context.
     Get the point from the value, then release the value because we retained it in touchesBegan:withEvent:.
     */
    NSValue *touchPointValue = (NSValue *)context;
    placardView.center = [touchPointValue CGPointValue];
    [touchPointValue release];
    [UIView commitAnimations];
    }
    
    0 讨论(0)
  • 2020-11-27 14:17

    There is absolutely no difference between these.

    0 讨论(0)
  • 2020-11-27 14:19

    There is no difference, however you should be aware that only the first "token" (so to speak) defines the type name, and the * is not part of the type name. That is to say:

    NSString *aString, bString;
    

    Creates one pointer-to-NSString, and one NSString. To get both to be pointers, do either:

    NSString *aString, *bString;
    

    or:

    NSString *aString;
    NSString *bString;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题