-4

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

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

谢谢你

4

1 回答 1

4

在最简单的情况下:

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:当只进行简单的连接时,相对昂贵。

于 2013-09-24T14:43:18.407 回答