Could anyone tell me the difference between
NSString* string;
And
NSString* string = [NSString string];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just like in C,
declares the variable (pointer)
stringbut gives it no value. This means you can’t use the variable until you’ve initialized it, for example by doingstring = @"foo";.Note:
nil.ifconditions, etc.) then the compiler will complain. To avoid this you could just set it to an empty string ornilto start with.The line
uses the
+stringmethod of theNSStringclass to create an empty string. You could also use@"".(Further note: I recommend using
NSString *stringinstead ofNSString* stringbecause the former hides a syntax idiosyncrasy:NSString *string1, *string2;is the correct way to declare multiple pointer variables on one line.)