Adding multiple strings to a string

Farini picture Farini · Nov 13, 2011 · Viewed 8.6k times · Source

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 ?

Answer

Odrakir picture Odrakir · Nov 13, 2011

How about something like this:

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