xcode 将多个字符串添加到一个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8114346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:24:55  来源:igfitidea点击:

Adding multiple strings to a string

xcodeuitableviewnsstringmfmailcomposeviewcontrollernsmutablestring

提问by Farini

How do I add multiple strings to a string ? Whats the easiest way to do that ? If I don't want to create a new line of code every time I add something to a string, I'd like to do something like that :

如何将多个字符串添加到一个字符串?最简单的方法是什么?如果我不想每次向字符串添加内容时都创建新的代码行,我想做这样的事情:

    NSString *recipeTitle = [@"<h5>Recipe name: " stringByAppendingFormat:recipe.name, @"</h5>"];
    NSLog(@"%@", recipeTitle);

    // This shows: <h5>Recipe name: myrecipe
    // Where's the </h5> closing that header ? It will only show up with the next line of code

    recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];

    //my problem is that will result in more than 1k lines of programming

Do I have to necessarily add a new line appending the appended every time ? Is there a faster/more productive way to do that ?

我是否必须每次都添加一个新行附加附加?有没有更快/更高效的方法来做到这一点?

I'm trying to compose the email body with my tableview in it and that will result in a huge set of programming lines. Isthere anybody that could give me any hint or anything better than composing a huuuge string just so i can populate my email body with a table containing my tableview data ?

我正在尝试用我的 tableview 编写电子邮件正文,这将导致大量的编程行。有没有人能给我任何提示或比编写一个 huuuge 字符串更好的东西,这样我就可以用包含我的 tableview 数据的表格填充我的电子邮件正文?

Any help to make this more productive is appreciated. Thanks ! Carlos Farini.

任何有助于提高生产力的帮助表示赞赏。谢谢 !卡洛斯·法里尼。

// After working on it a bit i got:

// 做了一点工作后,我得到了:

-(IBAction)sendmail{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
NSString *recipeTitle = @"<h5>Recipe name: ";
recipeTitle = [recipeTitle stringByAppendingFormat:recipe.name];
recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];

NSString *ingredientAmount = @"";
NSString *ingredientAisle = @"";
NSString *ingredientTitle = @"";

NSString *tableFirstLine = @"<table width='90%' border='1'><tr><td>Ingredient</td><td>Amount</td><td>Aisle</td></tr>";
NSString *increments = @"";
int i=0;

for (i=0; i < [ingredients count]; i++) {
    Ingredient *ingredient = [ingredients objectAtIndex:i];
    ingredientTitle = ingredient.name;
    ingredientAmount = ingredient.amount;
    ingredientAisle = ingredient.aisle;

    increments = [increments stringByAppendingFormat:recipeTitle];
    increments = [tableFirstLine stringByAppendingFormat:@"<tr><td>"];
    increments = [increments stringByAppendingFormat:ingredientTitle];
    increments = [increments stringByAppendingFormat:@"</td><td>"];
    increments = [increments stringByAppendingFormat:ingredientAmount];
    increments = [increments stringByAppendingFormat:@"</td><td>"];
    increments = [increments stringByAppendingFormat:ingredientAisle];
    increments = [increments stringByAppendingFormat:@"</td></tr>"];
    if (i == ([ingredients count]-1)) {
        //IF THIS IS THE LAST INGREDIENT, CLOSE THE TABLE
        increments = [increments stringByAppendingFormat:@"</table>"];
    }
}

NSLog(@"CODE:: %@", increments);

if ([MFMailComposeViewController canSendMail]) {
    [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
    [composer setSubject:@"subject here"];
    [composer setMessageBody:increments isHTML:YES];
    [composer setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:composer animated:YES];
    [composer release];
}else {
    [composer release];
}

}

}

But then again, it's showing just one row in the table. What am I doing wrong here ?

但话又说回来,它只显示表格中的一行。我在这里做错了什么?

采纳答案by Odrakir

How about something like this:

这样的事情怎么样:

NSString *recipeTitle = [NSString stringWithFormat:@"<h5>Recipe name: %@ </h5>", recipe.name];