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.
Barry Wark Jul 09 '09 at 19:48 2009-07-09 19:48
source share