This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Placement of the asterisk in Objective-C
I am confused, is there a difference between declaring a pointer as NSString* myVar; and NSString *myVar; ?
i.e. is the location of the asterix significant?
No, there is no difference. However I think that 2nd one is more readable.
int* a, b;
int *a, b;
In the 2nd one it is clear that a is a pointer but b is not. But in the 1st line it looks like both a and b are pointers which is not true.
But still, this is a personal choice and there is NO difference from compiler's point of view in the two lines.
No.
Related
This question already has answers here:
What's the difference between NSNumber and NSInteger?
(5 answers)
Closed 6 years ago.
I have an NSInteger that doesn't use a pointer and an NSNumber that does use a pointer. Can someone explain to me why this is the case? All my teacher said was the NSInteger is being used as a type alias but I'm not familiar with that either yet.
This question asked a direct question asking for the reason there was no pointer in NSInteger; not asking for all the differences.
Bear in mind that Objective-C is C. Thus:
An NSInteger is a scalar, a built-in C data type (an integer). [The actual size of this integer depends on the architecture, 32-bit vs. 64-bit. But it is still some form of C integer.]
An NSNumber is an object; Objective-C object references are represented as C pointers.
This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 7 years ago.
I want to understand the syntax of the struct which i have seen some where. Can some please explain the meaning of unsigned int xyz:1;. Is it just assigning default value to a variable xyz? BTW this code is in Objective C.
struct
{
unsigned int xyz:1;
} testStruct;
It's a bit field. You are telling the structure that you will only be using one bit of xyz.
This allows the compiler to make packing optimisations.
This question already has answers here:
Pointers on Objective-c
(2 answers)
Why dereferencing a NSString pointer is not necessary?
(1 answer)
Closed 9 years ago.
The following snippet prints the value 10,
int x = 10;
int *y = &x;
NSLog(#"Value pointed by y = %d",*y);
But incase of NSString pointer as below, why are we not prefixing the variable name with * to retrieve the value it points to.
NSString *country = #"USA";
NSLog(#"Value pointed by country: %#",country);
Let me know if I am missing something. Thanks.
You can think about this as of a purely syntactic convention - a way of keeping Objective-C a pure superset of the C language. The way the Objective-C designers decided to represent objects is with C pointers: all variables of object type, including NSString, must be declared as a pointer. In fact, it is an error to define a variable of an object type without an asterisk:
NSString country; // <<== ERROR: this will not compile
Objects are always addressed by using pointers. You can not dereference them. Therefore is is not necessary and would be wrong to dereference an object pointer.
This question already has answers here:
Caret in objective C
(3 answers)
Closed 9 years ago.
There is a piece of code like
typedef void (^SignIn) (NSString *email, NSString *password);
What does the ^ mean before SignIn? Is this Objective-C specific usage?
It's the syntax for blocks.
That typedef declares SignIn to mean a block which takes two NSString* arguments and returns void (i.e. nothing).
It is a block.
For a guide to understanding blocks, see this tutorial
Unless, you already know what a block is, and you just didn't know what the caret was for.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's your preferred pointer declaration style, and why?
In C, why is the asterisk before the variable name, rather than after the type?
What makes more sense - char* string or char *string?
When declaring a new instance of an object in Objective-C, does it make any difference where you put the asterisk?
Is this just a matter of personal preference?
NSString* string = #"";
vs.
NSString *string = #"";
It doesn't make a difference, but there are good reasons to put it in each place:
It makes sense to put it near the class, because that makes it feel like a type: NSString*, a pointer to a string. Sensible.
It makes sense to put it near the variable, because that's what's actually happening: * is dereference. When you dereference your pointer string, you get an NSString. *string is an NSString. Sensible.
You may want to go with the latter because that's the way the compiler is thinking, so: NSString* oneString, anotherString will not work, whereas NSString *oneString, *anotherString is correct.
It's simply a matter of preference. Putting the * next to the type emphasizes that it's part of the type, i.e. "pointer to an NSString". However, this is usually frowned upon, because it ignores the fact that the * associates with the nearest variable name, not the type name. For instance, the following doesn't work:
NSString* a = #"string1", b = #"string2
This is because a is a pointer, but b is not.
Putting the * next to the variable name is, in my opinion, more of a C/C++ convention, because it emphasizes that the * and the variable name together act kind of like a variable.
Personally, I put a space on both sides of the *.
Another question that asked the same thing is here:
Declaring pointers; asterisk on the left or right of the space between the type and name?
It doesnt make the difference wher you put that pointer symbol. If you declare multiple objects in single line, you do it like NSString *str1, *str2. So its more appropriate to put that asterisk close to object. I prefer it close to object instance.