在目标c中将字符串连接在一起

时间:2013-09-24 14:42:20

标签: iphone ios objective-c

如何在目标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

对此的任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:3)

在最简单的情况下:

NSString* concatenatedString = [stringA stringByAppendingString: stringB];

从你的代码中,这个:

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

可能更好地表达为:

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

+stringWithFormat:比较昂贵,只有简单的连接才会发生。