Reputation: 4746
I've got a UIAlertView with a text field (alertView). Let's just say I type in "Asdf" in the UIAlertView text field.
In the first line, I grab the text from that field and create *myString
from it.
NSString *myString = [alertView textFieldAtIndex:0].text;
On the 2nd line, I display this string in the NSLog with an addition.
NSLog(@"My String is: %@", myString);
This displays properly in the NSLog:
My String is: Asdf
So then on the 3rd line I create an NSString with this addition + my original string (myString). Finally on the 4th line I display this second string in the NSLog.
NSString *mySecondString = (@"My String is: %@", myString);
NSLog(@"%@", mySecondString);
On the 3rd line (mySecondString = ...
), I get a warning saying
Expression result unused
The NSLog for this displays improperly as such:
Asdf
I'm sure I'm missing something very simple so if you spot something it would be much appreciated.
Upvotes: 0
Views: 235
Reputation: 6831
That's not how you concatenate strings in Objective-C
To properly concatenate two NSString
instances, there are a few options.
Option #1. +stringWithFormat
class method.
NSString *a = @"hello";
NSString *b = @"world";
NSString *c = [NSString stringWithFormat:@"%@%@", a, b];
Option #2, -stringByAppendingString
instance method.
NSString *a = @"hello";
NSString *b = @"world";
NSString *c = [a stringByAppendingString:b];
In your case, you could use the following.
NSString *mySecondString = [NSString stringWithFormat:@"%@%@",@"My String is:", myString];
Upvotes: 2