-4

how to i concatenate strings in objective c?

- (IBAction)emailButton:(id)sender { MFMailComposeViewController *mailContoller = [[MFMailComposeViewController alloc]init]; [mailContoller setMailComposeDelegate:self]; NSString *email = @"*******gmail.com"; NSString *email1 = @"*******@hotmail.co.uk"; NSArray *emailArray = [[NSArray alloc]initWithObjects:email, email1, nil]; NSString *message = [NSString stringWithFormat:@"%@\n%@\n%@", textField1.text, textField2.text, textField3.text]; [mailContoller setMessageBody:message isHTML:NO]; [mailContoller setToRecipients:emailArray]; [mailContoller setSubject:@"IT WORKS!"]; [self presentViewController:mailContoller animated:YES completion:nil]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self myTextView] resignFirstResponder]; } -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; } @end 

any help on this will be greatly appreciated.

thank you

1
  • @DavidRönnqvist Yeah. Deleted it, though. Commented Sep 24, 2013 at 15:07

1 Answer 1

4

In the simplest case:

NSString* concatenatedString = [stringA stringByAppendingString: stringB]; 

From your code, this:

NSString *message = [NSString stringWithFormat:@"%@\n%@\n%@", textField1.text, textField2.text, textField3.text]; 

might be better expressed as:

NSString *message = [@[textField1.text, textField2.text, textField3.text] componentsJoinedByString: @"\n"]; 

+stringWithFormat: is comparatively expensive when there's nothing but simple concatenation going on.

Sign up to request clarification or add additional context in comments.

2 Comments

i just keep getting a message saying undeclared variable when i input this code.
Naturally, you need to replace stringA and stringB with your string variables.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.