Note that there are some explanatory texts on larger screens.

plurals
  1. POSave contents of NSMutableArray to file
    primarykey
    data
    text
    <p>I have an app which has several <code>ViewControllers</code>. Each <code>ViewController</code> has a question with 2-5 <code>buttons</code> as possible answers. The title of a button is saved to a <code>NSMutableArray</code> called <code>submission</code>. The user can go back to previous questions, change their answer, and the <code>NSMutableArray</code> will be updated accordingly. I need to save this array to file so new results can be saved into a <code>UITableView</code> each time the questionnaire is completed. I have researched and it sounds like a .plist is a good option, as all of my objects in the array are <code>NSStrings</code>.</p> <p>A sample of my array:</p> <pre><code>2013-12-17 14:06:34.210 Questionnaire[1724:70b] ( 1234, "Dec 17, 2013", Yes, Games, "Not Applicable", Yes ) </code></pre> <p>"1234" is the User ID, the date is the Date of Birth, and the other submissions are the answers to each question. </p> <p>My ViewControllers look like this:</p> <pre><code>MainViewController InfoViewController &lt;-- Array allocated + initialised, inserting ID and DoB Q1ViewController &lt;-- question .. Q4ViewController &lt;--question ENDViewController &lt;-- offers user options for Home or Results ResultsViewController &lt;-- UITableView ordered by User ID SavedResultsViewController &lt;-- UITableView showing complete submission </code></pre> <p>The <code>NSMutableArray</code> gets passed through each ViewController. </p> <p>My questions: What method of saving to file would best suit my needs? (.plist, filetype, etc). Viewing the results on Excel would be nice (but not essential). Where should the save take place? I was thinking when the last object is inserted into the array on Q4 <code>ViewController</code>, so it would be saved to file when the <code>ENDViewController</code> is popped, is this logical? Do I need to create a new <code>Objective-C</code> file to store the data? I have saw a few tutorials explaining this, declaring each object in a separate <code>NSObject</code> file, although I'm not sure if that is needed as my objects are already stored in the array. </p> <p>As you've probably guessed I am quite new to Objective-C and iOS programming, so any help offered is greatly appreciated. I am not sure how else to describe my problem, so apologies if the question is still unclear.</p> <p>Edit: I have learnt a bit more about Objective-C since creating this post, and have decided to save my data to a .csv file. This file is strictly for the purpose of emailing, it doesn't get displayed on the <code>UITableView</code> (I am currently implementing <code>Core Data</code> for that). This solution might help someone in the future, so here it is:</p> <pre><code>// Set column titles for .csv NSArray *columnTitleArray = @[@"User ID", @"DoB", @"Question 1", @"Question 2", @"Question 3", @"Question 4"]; NSString *columnTitle = [columnTitleArray componentsJoinedByString:@","]; NSString *columnTitleToWrite = [columnTitle stringByAppendingString:@"\n"]; // Separate submissions from array into cells NSString *questionResults = [self.submission componentsJoinedByString:@","]; NSString *questionResultsToWrite = [questionResults stringByAppendingString:@"\n"]; // Find documents directory NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // Set file name and type NSString *survey = [docPath stringByAppendingPathComponent:@"result.csv"]; // Create new file if none exists if (![[NSFileManager defaultManager] fileExistsAtPath:survey]){ [[NSFileManager defaultManager] createFileAtPath:survey contents:nil attributes:nil]; // Set column titles for new file NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:survey]; [fileHandle writeData:[columnTitleToWrite dataUsingEncoding:NSUTF8StringEncoding]]; } NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:survey]; [fileHandle seekToEndOfFile]; [fileHandle writeData:[questionResultsToWrite dataUsingEncoding:NSUTF8StringEncoding]]; [fileHandle closeFile]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload