I want to know the difference between nil and @"" in NSMutableString.
I need to clean string value in NSMutableString every second.
So
myMutableString = nil;
or
myMutableString = @"";
Which one is better to clean and why?
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.
UPDATE
In the case of a mutable string, you have to alloc/init it first like this:
Maybe you have done that, but then you have to reset the string like this:
So instead of writing
myMutableString = @"", use the code above.If you assign
myMutableStringtonilit is not a valid pointer/object and cannot respond to messages or actions. If you actually set it to@"", it is a totally valid object which can respond to messages, methods and actions, it is just contains a string with a length of 0.This cannot happen since the string is
nilThis can happen,
myMutableStringis a valid object and can respond to messages. And guess what, it now has a string!So, a string object can still be initialized and have have an actual string value without any characters. Just like an array can be valid and have 0 objects inside it. Otherwise, how would you add to it!?
However, In an
NSMutableString‘s scenario, you may have to actually alloc-init it…. somebody please clarify.Obviously, assigning to
@""is better, it actually depends on your scenario though. I don’t know why you would want to assign tonil, unless you are reassigning the variable to a new string object.