【问题标题】:Adding multiple strings to a string将多个字符串添加到字符串
【发布时间】:2011-11-13 20:07:47
【问题描述】:

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

    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

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

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

感谢任何有助于提高工作效率的帮助。谢谢 ! 卡洛斯·法里尼。

// 稍微研究了一下,我得到了:

-(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:@"123@abc.com", nil]];
    [composer setSubject:@"subject here"];
    [composer setMessageBody:increments isHTML:YES];
    [composer setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:composer animated:YES];
    [composer release];
}else {
    [composer release];
}

}

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

【问题讨论】:

    标签: xcode uitableview nsstring mfmailcomposeviewcontroller nsmutablestring


    【解决方案1】:

    这样的事情怎么样:

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

    【讨论】:

    • 我可以放多个 %@ 然后像 ..., recipe.name, recipe.preptime, recipe.included 吗?我将在同一行代码中附加三个字符串。那也不错……
    • 是的,你可以。您也可以查看文档以了解更多生成字符串的方法。
    • 太完美了。感谢您的启发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2012-10-04
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多