1

Is there a difference between these two declarations? Is one better practice?

  • NSString* hello = @"HelloWorld";
  • NSString *hello = @"HelloWorld";

Thanks--

2 Answers 2

4

There is absolutely no difference, although the second one I find is more popular amongst Objective-C developers.

Sign up to request clarification or add additional context in comments.

Comments

3

dreamlax's answer is correct but I would like to make the things more clear, the compiler strips whitespaces, both examples will be converted to NSString*hello=@"HelloWorld"; so there is no difference at all, use the one you feel more comfortable with. I prefer the second one, because its more clear (I read all declarations from right to left):

NSString *hello = @"HelloWorld";
^        ^        ^
3        2        1

1 => We have string value 2 => Pointer variable pointing to the address in memory where our object value is stored 3 => Object is of type NSString

(:

1 Comment

Appreciate the clarification- I prefer the second as well- It makes sense when broken down syntactically, as you illustrated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.