Difference between nil and #"" in NSMutableString iOS - ios

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?

UPDATE
In the case of a mutable string, you have to alloc/init it first like this:
NSMutableString *myMutableString = [[NSMutableString alloc] init];
Maybe you have done that, but then you have to reset the string like this:
[myMutableString setString: #""];
So instead of writing myMutableString = #"", use the code above.
If you assign myMutableString to nil it 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.
myMutableString = nil;
[myMutableString appendString: #"It now contains a valid string!"];
This cannot happen since the string is nil
myMutableString = #"";
[myMutableString appendString: #"It now contains a valid string!"];
This can happen, myMutableString is 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 to nil, unless you are reassigning the variable to a new string object.

None of them
use
[myMutableString setString: #""];
to reset your string.
Your object remains the same. You invoke a method that clear its content.

Related

How can i implement the same behaviour as in cluster pattern by apple (NSString and NSCFString)

I am simply writing the following code for testing purpose:
NSString *aStr = [[NSString alloc] initWithFormat:#"Foo"];
aStr = [aStr initWithFormat:#"Bar"];//Crashed here
I am getting the following error:
*** initialization method -initWithFormat:locale:arguments: cannot be sent to an abstract object of class __NSCFString: Create a concrete instance!
If i write the following code same thing happen
NSString *aStr = [NSString alloc];
aStr = [aStr initWithFormat:#"Foo"];
aStr = [aStr initWithFormat:#"Bar"]; //Crashed here
By google I come to know that initWithFormat will return the NSCFString Object.
My question is if NSCFString is derived class of NSString then why I cannot invoke the initWithFormat method on NSCFString. If it is possible to stop the visibility how can I implement in the code without overriding the method in NSCFString(Derived Class).
In simple word If NSCFString is derived class of NSString then why i cannot call the base class (initWithFormat) method on that?
I believe that what's happening is that the [NSString initWithFormat:] method is noticing that you have not provided any format specifiers so there is no NSString object that needs building, so it's simply returning the constant NSString object you pass in (#"Foo"):
NSString *aStr = [NSString alloc];
aStr = [aStr initWithFormat:#"Foo"];
So aStr is now of type NSCFString. This is the cause of the crash:
aStr = [aStr initWithFormat:#"Bar"]; //Crashed here
However you should never be calling an init method on an existing object, so to correct the crash use:
aStr = [[NSString alloc] initWithFormat:#"Bar"];
and use format specifiers as you may as well just do:
aStr = #"Foo";
aStr = #"Boo";
which is the same thing only clearer, uses less code and has better performance.
As per Documentation it returns an NSString object initialized by using a given format string as a template into which the remaining argument values are substituted.
And also you need to use like that below :-
NSString *aStr = [[NSString alloc] initWithFormat:#"%#,%#",#"Foo",#"Bar"];
NSLog(#"%#",aStr);
You are asking "why". Besides the answer "calling init twice on the same object is baaaad" which you ignored, NSString is a class cluster.
[NSString alloc] returns a generic object that isn't really usable as a string but expects an init method to be called. Let's think about something obvious: NSString objects are immutable, but the result of [NSString alloc] cannot be immutable, because otherwise it would be impossible to store a value, right?
That init method will actually return a different object that doesn't accept init methods anymore. While [NSString alloc] is a very flexible object that could do anything, after a call to an init method you get an object back that contains a string that can never be modified again.
(Apple may have implemented this differently from what I say or may change their implementation. Nevertheless, they can and will do things that stop you from doing something stupid).
And a warning: Don't even think about subclassing NSString. I can guarantee that you won't get anything cobbled together that works.

Adding NSString to NSMutableString

I want to add multiple NSStrings yo an NSMutableString to construct a list of strings.
This is my code in a method that takes an NSDictionary and grabs the value out of it - it then stores it in an NSString, From there I append the string to the NSMutableString. When running this method for the first time it works. However, when I call the method again, it replaces the last string that was in the NSMutableString.
Here is the code:
NSString *userId = [NSString stringWithFormat:#"%#,", parameters[#"userId"]];
self.alluserIds = [NSMutableString string];
[self.alluserIds appendString:userId];
Can anyone tell me what I am doing wrong?
self.alluserIds is declared strong.
This:
self.alluserIds = [NSMutableString string];
is creating a new mutable string. It should be done once, before you want to use the mutable string, and not done again until you want to restart (because recreating the mutable string destroys the old instance you had).
This is happened because you call:
self.alluserIds = [NSMutableString string];
This create new string and assign it to self.alluserIds. You should call it just once in for example init method or viewDidLoad.
You should initialize with the string you are using and it should be a single initialization your code should look like below.
NSString *userId = [NSString stringWithFormat:#"%#,", parameters[#"userId"]];
if(!self.alluseIds)
self.alluserIds = [[NSMutableString alloc] initWithString:userId]; //ONLY Initialization
else
[self.alluserIds appendString:userId];
Hope this helps
I don't know your data-structure, but if you might have all userids in an array you could join them together with componentsJoinedByString::
NSArray *pathArray = [NSArray arrayWithObjects:#"here", #"be", #"dragons", nil];
NSLog(#"%#",[pathArray componentsJoinedByString:#" "]);
Don't use mutable strings(and mutable collections too) except cases you really need it: http://rypress.com/tutorials/objective-c/data-types/nsstring.html (When To Use Mutable Strings) You have to use them when you want to make a lot of operations. Immutable collections are slower because you create new instance when append another string.
How to do this with NSString:
self.alluserIds = [self.alluserIds stringByAppendingString: userId];

Multiple NSString declaration and initialization

I am new to the ios development.I was checking the NSString and where I had found out that it was excessed using the multiple ways which is as under
1) NSString *theString = #"hello";
2) NSString *string1 = [NSString stringWithString:#"This is"];
3) NSString *string =[[NSString alloc]init];
string =#"Hello";
Now I am confused with above three and would like to know the differences between then?
Yes all three are ways to create string...I try to explain you one by one.
One think you must remember that in Objective-c string is represented by #"".
whatever comes double quotes is string eg #"Hello" is string. #"" is basically literal to create NSString.
NSString *theString = #"hello";
EDIT:
In this statement we are creating an NSString constant. Objective-C string constant is created at compile time and exists throughout your program’s execution.
2. NSString *string1 = [NSString stringWithString:#"This is"];
In this statement again, we are creating an autorelease object of NSString, but here a slide diff. than the first case. here we are using another NSString object to create a new autorelease object of NSString. So generally we use stringWithString method when we already have NSString Object and we want another NSString object with similar content.
3. NSString *string =[[NSString alloc]init];
string =#"Hello";
Here, In first statement you are creating a NSString Object that you owned and it is your responsibility to release it (In non-ARC code), once you done with this object.
Second statement is similar to case 1, by string literal you are creating string Object.
but when you put these two statements together it causes you memory-leak(In non-ARC code), because in statement one you are allocating & initiating memory for new string object and right after in second statement you are again assigning a new string object to the same string reference.
1) and 2) is the same. there is no difference. [NSString stringWithString: str] just does nothing and returns str if str is immutable already. via this link
3) you create empty NSString pointer. after you assign this string with ' = ' is same way with 1) and it alloc memory to your NSString. You can watch this NSString pointer in 3) and you see pointer was changed:
NSString *string =[[NSString alloc]init];
printf("\n string_1-> = %p\n", string );
string = #"Hello";
printf("\n string_2-> = %p\n", string );
OK, first off, the syntax #"blah" is an Objective-C literal to create an NSString. Essentially it is an NSString.
Also, there are two parts to each statement. The declaration of the string variable NSString *string and the instantiation string = <something>.
So...
This is how you would do it most of the time. You declare the variable and set it using the objective-c literal.
This adds a bit of redundancy and you wouldn't really use it like this. You'd maybe use it with a substring or something from another string. As it is you are essentially creating two objects here. The first is [NSString stringWithString:<>]; and the second is #"This is".
This is a bit odd. You are first creating an empty string #"" and then you are replacing this string with #"Hello". Don't do this. There is no point. You may as well just do number 1 as the result is the same.
All three will work but in these exact cases you'd be better off using the first.

Objective C why format string as string

While working on project code left to me by a previous dev, I have encountered the following construct
-(NSString *)StringCheckWithString:(NSString *)string{
NSString *string2 = [NSString stringWithFormat:#"%#", string];
if([string2 length] == 0){
return #"none";
}
else {
return string2;
}
}
Can anyone explain why you would do this, it seems significantly overengineered to me and I don't understand why it has been done this way (for clarity, I don't understand why the string is formatted like this, I understand the length check)
The argument that is passed in could be any subclass of string, including NSMutableString. This code creates an immutable copy of it. This means that you can store the returned string without having to worry about someone else modifying it.
A better way of doing this would be:
NSString *string2 = [string copy];
According to the NSCopying Protocol reference:
The copy returned is immutable if the consideration “immutable vs.
mutable” applies to the receiving object.

Output to UITextField

I have a NSDictionary with some parameters i want to display in a UITextField.
but
firstname.text = [userdata objectForKey:#"firstname"];
throws an exeption.
If i use NSLog on [userdata objectForKey:#"firstname"]; it shows the right value. What seems to be the problem here?
Just use the following code:
firstname.text = [NSString stringWithFormat:#"%#",
  [userdata objectForKey:#"firstname"]];
go on .. :)
When you NSLog something it uses [object description] to convert the object to a string for output. If the object stored in your dictionary is not an NSString and you try to assign to firstname.text you will get an exception.
Another problem you may be facing is your firstname object may not be pointing at what you think it is. This may occur, for example, if you are not using ARC and you forgot to retain it or you already released it.
The exact exception you are seeing will determine which of these you are encountering.
Tried to do something like
[NSString stringWithFormat:#"%#",[userdata objectForKey:#"firstname"]];
firstname.text = nameString;
?

Resources