Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

While this isn't a problem, I wanted to hear if someone could please check the format string I use when I create an CSV file? It does work, but it looks kind of "weird" and was wondering if this was the optimal format?

These are the column headers:

NSMutableString *csv = [NSMutableString stringWithFormat:@"Title 1,Title 2,Title 3,Title 4,Title 5,Title 6,Title 7"];

Then I iterate over an array I have to populate the CSV file:

for (int i = 0; i < [myArray count]; i++)
{
    [csv appendFormat:@"\n\"%@ %@%@ %@\",%@,%@,%@,%@,%@,\"%@\"",
@"value one", @"value two", @"value three", @"value four", // this is one column
@"Value one", // this is a new column
@"value two", // this is a new column
@"value three", // this is a new column
@"value four", // this is a new column
@"value five", // this is a new column
@"value six" // this is a new column
}

Then this is where I create the file and save it in the document directory:

    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array lastObject];
NSString *fileName = [docDir stringByAppendingPathComponent:@"export_file.csv"];
NSError *error;
BOOL res = [csv writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];

All of the "value x" strings are replaced by functions in this question for the sake of simplicity.

Is the format string correct, and is this a good way of creating a CSV file to add as attachment to a MFMailComposeViewController's mail?

If I repeat this process without changing the file name, will it simply overwrite the existing file? It sure looks like it, but I just want to be sure.

share|improve this question

closed as off-topic by nhgrif, jacwah, Ethan Bierlein, Hosch250, Vogel612 Aug 15 at 22:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
Hypothetical code is off-topic for Code Review. And it seems the part you're primarily asking about is purely hypothetical. For instance, one of the things I'd comment on is the type of loop you're using... which seems wrong... but since I don't actually know what's happening in that loop, I can't really make any comment on that part of your code. –  nhgrif Aug 15 at 20:46
    
@nhgrif It just gets a custom object like (MyObject *)myArray[i]; and grabs a few strings from it really - the ones I've hardcoded in this example. I could use for (MyObject *currentObject in myArray) instead indeed. I just used the other type (the one in the question) during testing to log out i in the loop. I'm just asking whether the format I'm using in appendFormat: is correct, given the strings I provide underneath. The strings will only contain letters, no symbols. It'll create a table with 7 columns and x (array count) number of rows as a CSV format for use with excel or similar –  Erik Aug 15 at 21:56

Browse other questions tagged or ask your own question.