I am completely new to Objective-C and although i have some experience with java and C#, I just can't get this to work.
My code is:
- (IBAction)btnClickMe_Clicked:(id)sender {
Label_1.text = (#"some string" + _Label_2.text);
}
I am also curious as to why Label_1 does not need an underscore infront of it, like _Label_2 does?
To concatenate strings, you use
Label_1.text = [#"Some string" stringByAppendingString:_Label_2.text];
You can use %# to append your additionnals strings with stringWithFormat
Label_1.text = [NSString stringWithFormat: #"Some string %#", _Label_2.text];
More example : Apple - Formatting String Objects
NSString provides a vast variety of methods for string manipulations. Amongst them are several ways for conatination.
You should get familiar with the factory method stringWithFormat. It is one of the most powerful and especially good at a bit more complex requirements.
In your case:
Label_1.text = [NSString stringWithFormat:#"Some string%#", _Label_2.text);
or
Label_1.text = [NSString stringWithFormat:#"%#g%#", #"Some string", _Label_2.text);
The format string corresponds to the usual standard c printf format string plus the %# tag which is replaced by any objects description value. So you could have an NSNumber there or even an NSArray or so. However, the description of NSArray, NSDictionary, NSSet etc. may not really be useful for production but come quite handy for debugging. NSLog() uses the same format.
Related
this is a more theoretical question than pratical;
Assuming that i have two Strings, one normal and one mutable, if i do:
NSMutableString *prova = [[NSMutableString alloc] init];
NSString *prova2 = [[NSString alloc] init];
[prova appendFormat:#"%#",#"CIAO"];
prova2 = prova;
NSLog(#"String: %#",prova);
NSLog(#"String2: %#",prova2);
[prova setString:#""];
NSLog(#"-String: %#",prova);
NSLog(#"-String2: %#",prova2);
Then the result is:
2013-04-22 22:01:53.604 CodeTest[6974:303] String: CIAO
2013-04-22 22:01:53.605 CodeTest[6974:303] String2: CIAO
2013-04-22 22:01:53.605 CodeTest[6974:303] -String:
2013-04-22 22:01:53.606 CodeTest[6974:303] -String2:
Why does it behave like this?
I just wanna find it out, because i've never encountered such thing before while programming in python, php or C++ (I think i was never confronted with so many different data types as in Obj-c :P )
Thanks
Just in case anyone wants to know how to get over it here is the right code i used to assign the value to the string without losing it after the "blanking" (sostitute to prova2 = prova):
prova2 = [NSString stringWithString:prova];
Remember that the NSString and NSMutableString variables are pointers. That means when you assign prova2 = prova you're really changing prova2 to point to prova, and the value you assigned to prova2 is lost.
So when you assign the empty string to prova both variables point to #"".
prova2 = prova;
This doesn't assign the contents of the string in prova to the string in prova2, it just makes prova2 point to the same object as prova, effectively leaking the string that prova2 used to point to. So even though prova2 is declared as a NSString*, it now points to a NSMutableString.
In addition to Richard Brown's great response, that's what you'd like to do instead for desired behaviour:
[prova2 setString: prova];
instead of
prova2 = prova;
For example I'd like to replace all occurrences of #"a" and #"b" in #"abcdabcd" with #"z". I'm currently doing this with repeated called to stringByReplacingOccurencesOfString:withString::
NSString *s1 = #"abcdabcd";
NSString *s2 = [[s1 stringByReplacingOccurencesOfString:#"a" withString:#"z"]
stringByReplacingOccurencesOfString:#"b" withString:#"z"];
What's a better way? I didn't find any similar methods that take an array of strings to replace.
You can use regular expressions:
NSString *s2 =
[s1 stringByReplacingOccurrencesOfString:#"[ab]"
withString:#"z"
options:NSRegularExpressionSearch
range:NSMakeRange(0, s1.length)];
There's also NSMutableString's replaceOccurrencesOfString:withString:options:range: method (so you don't have to create a new NSString object for every replacement call you want to make). Documentation linked for you.
NSString *string = #"HELLO";
For some reason, XCode won't auto-complete methods like remove characters or append etc... If that's the case, how can I, say, remove certain characters from my string? Say I want to remove all the L's.
NSString doesn't respond to those methods. NSMutableString does, but you've declared an immutable string variable and assigned to it a string literal. Since an Objective-C #"string literal" is always immutable (an instance of NSString but not NSMutableString), there's no way those messages can be sent to the object you're using.
If you want a mutable string, try:
NSMutableString *mutableString = [[#"HELLO" mutableCopy] autorelease];
That's an immutable string literal.
Here is a great post explaining it in further details:
What's the difference between a string constant and a string literal?
As for your question on how would you change it and remove the Ls:
NSString *hello = #"HELLO";
NSString *subString = [hello stringByReplacingOccurrencesOfString:#"L" withString:#""];
NSLog(#"subString: %#", subString);
That outputs "HEO"
Either that or you can create an NSMutableString by creating a copy of the mutable string like Jonathan mentioned. In both examples, you're copying it into a non-literal string.
I have an NSString and fail to apply the following statement:
NSString *myString = #"some text";
[myString stringByAppendingFormat:#"some text = %d", 3];
no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString.
any clues most welcome.
I would suggest correcting to (documentation):
NSString *myString = #"some text";
myString = [myString stringByAppendingFormat:#" = %d", 3];
From the docs:
Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.
It's working, you're just ignoring the return value, which is the string with the appended format. (See the docs.) You can't modify an NSString — to modify an NSMutableString, use -appendFormat: instead.
Of course, in your toy example, you could shorten it to this:
NSString *myString = [NSString stringWithFormat:#"some text = %d", 3];
However, it's likely that you need to append a format string to an existing string created elsewhere. In that case, and particularly if you're appending multiple parts, it's good to think about and balance the pros and cons of using a mutable string or several immutable, autoreleased strings.
Creating strings with #"" always results in immutable strings. If you want to create a new NSMutableString do it as following.
NSMutableString *myString = [NSMutableString stringWithString:#"some text"];
[myString appendFormat:#"some text = %d", 3];
I had a similar warning message while appending a localized string. This is how I resolved it
NSString *msgBody = [msgBody stringByAppendingFormat:#"%#",NSLocalizedString(#"LOCALSTRINGMSG",#"Message Body")];
I have a fairly simple question concerning NSString however it doesn't seem to do what I want.
this is what i have
NSString *title = [NSString stringWithformat: character.name, #"is the character"];
This is a line in my parser takes the charactername and inserts in into a plist , however it doesn't insert the #"is the character" is there something I'm doing wrong?
Your code is wrong. It should be :
NSString *title
= [NSString stringWithformat:#"%# is the character", character.name];
assuming that character.name is another NSString.
Read the Formatting String Objects paragraph of the String Programming Guide for Cocoa to learn everything about formatting strings.
stringWithFormat takes a format string as the first argument so, assuming character.name is the name of your character, you need:
NSString *title = [NSString stringWithformat: #"%s is the character",
character.name];
What you have is the character name as the format string so, if it's #"Bob" then Bob is what you'll get. If it was "#Bob %s", that would work but would probably stuff up somewhere else that you display just the character name :-)
Note that you should use "%s" for a C string, I think "%#" is the correct format specifier if character.name is an NSString itself.