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

发布于 2024-12-15 09:59:57 字数 3060 浏览 2 评论 0原文

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

    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

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

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

任何有助于提高工作效率的帮助都将受到赞赏。谢谢 ! 卡洛斯·法里尼.

// 经过一番研究后,我得到:

-(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];
}

}

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

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 ?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2024-12-22 09:59:57

像这样的事情怎么样:

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

How about something like this:

NSString *recipeTitle = [NSString stringWithFormat:@"<h5>Recipe name: %@ </h5>", recipe.name]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文