Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective - C Rookie NSString Problem

I have this code:

// Fill out the email body text
NSString *emailBody = (@"Name:%@\nNumber of People:\nDate:", name.text);
NSLog(@"%@", emailBody);

As you can see I'm trying to append name.text to the e-mail body just after "Name:". However, NSLog only outputs the string contained in name.text and none of the rest of the e-mail body. What am I doing wrong here that the code deletes the rest of the string apart from name.text?

E.G if name.text contained the text "Jack", then NSLog would only output "Jack" and not:

Name: Jack
Number of People: x 
Date: x

Which is what I am looking for.

Can anyone give me an insight as to what I'm doing wrong?

Thanks,

Jack

like image 662
Jack Nutkins Avatar asked Dec 16 '22 13:12

Jack Nutkins


1 Answers

Use +stringWithFormat method:

NSString *emailBody = [NSString stringWithFormat:@"Name:%@\nNumber of People:\nDate:", name.text];

What you have now is a valid code, but it doesn't do what you want:

(@"Name:%@\nNumber of People:\nDate:", name.text);

calls a comma operator - it evaluates its 1st parameter, discards it and returns 2nd parameter, so that's why emailBody is eventually filled with name.text value

like image 114
Vladimir Avatar answered Jan 13 '23 22:01

Vladimir