Placing an asterisk in Objective-C

I just started to learn Objective-C based on the background of VB.Net and C # .Net. I understand the use of the pointer, but in the Objective-C examples, I see an asterisk placed in several different places, and, as far as possible, looking for a search, I could not find an answer about why this is so. Every search that I try causes all kinds of explanations about pointers (which I really donโ€™t need), but not a single mention of the causes / effects of different star locations. Here are some examples I've seen:

NSString *string; NSString * string; (NSString *) string; NSString* string; 

What do these different asterisk positions mean? I am sure that this is a simple answer, but it disappoints him that he cannot find it in any of Apple's manuals and reference documentation or on the Internet so far.

Can someone please end my misfortune and answer this puzzling question? Thank!

+52
pointers objective-c
Jul 09 '09 at 18:52
source share
14 answers

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

 NSString *aString, bString; 

Creates one pointer to NSString and one NSString . To display both pointers, do either:

 NSString *aString, *bString; 

or

 NSString *aString; NSString *bString; 
+32
05 Oct '09 at 17:34
source share
 1. NSString *string; 2. NSString * string; 3. (NSString *) string; 4. NSString* string; 

1, 2 and 4 are exactly identical. All is style. Choose what you want or mix.

Option number 3 has another meaning that it was used in the casting. For example:

 t = (NSString *)string ; 

maps string to an NSString pointer.

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

+23
Jul 09 '09 at 19:00
source share

There is no difference - it is a matter of style. They all declare a variable called string , a pointer to an NSString. Brackets are necessary in some contexts (especially for method declarations) to clarify that this is a type declaration.

+19
Jul 09 '09 at 18:56
source share

no matter where you place the asterisk, all instructions create pointers of type NSString.

when using multiple variable names on the same line, you need to write an asterisk for each variable.

 NSString * nsstring, * nsstring2; 
+6
Jul 09 '09 at 19:03
source share
 1. NSString *string; 2. NSString * string; 3. (NSString *) string; 4. NSString* string; 

1,2 and 4 are equivalent. The C language (and a superset of Objective-C C) indicates syntax that is insensitive to whitespace. Thus, you can freely add spaces in which you choose a style. All relevant syntax is separated by characters without spaces (for example, { , } ,; etc.) [1].

3 is either a cast type (telling the C compiler to use the NSString* type, regardless of the declared string type. In Objective-C, the input type of object instances is rarely needed. You can use id for variables that can reference instances of any type of object.

In method declarations, syntax 3 (sometimes without a semicolon) is used to declare the type of method parameters. The Objective-C method might look like this:

 - (void)myMethodThatTakesAString:(NSString*)string; 

In this declaration, the argument type named string is the type NSString* (leading - indicates the instance method that opposes the class method). A method declaration with more than one parameter might look like this:

 - (void)myMethodTakingAString:(NSString*)string andAnInteger:(NSInteger)intParam; 

[1] This is compared to languages โ€‹โ€‹like Python, which use spaces as a block.

+6
Jul 09 '09 at 19:48
source share

It makes no difference when * is placed in a pointer declaration, it does not matter.

+5
Oct 05 '09 at 17:27
source share

No difference, placing spaces does not matter.

+4
Oct 05 '09 at 5:30 p.m.
source share

There is absolutely no difference. NSString * mystring and NSString * myString are identical.

+4
Oct 05 '09 at 18:17
source share

in fact, everything is equivalent: a pointer to nsstring !!

+3
Jul 09 '09 at 18:55
source share

There is no difference between them.

+3
Oct 05 '09 at 17:28
source share

* works the same as in standard C.

This is a good tutorial on posting * in different places and what it does: http://boredzo.org/pointers/

+3
05 Oct '09 at 19:03
source share

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; although a valid operator, it does nothing 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 
+2
Jul 09 '09 at 19:06
source share

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
Jul 15 '11 at 18:16
source share

There can be no difference in alternatives 1, 2, and 4 on the computer, but there are other code readers.

Since the explanations are like https://stackoverflow.com/a/2129609/ and the https://www.tutorialspoint.com/objective_c/objective_c_pointers.htm uses the first option:

1. NSString *string;

and all additional variables need their own asterisk, for example:

NSString *aString, *bString;

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

0
Jan 05 '17 at 12:14
source share



All Articles