How to convert data to CSV or HTML format in iOS?

In my iOS application, I need to export some data to CSV or HTML format. How can i do this?

+1
source share
3 answers

RegexKitLite contains an example of how to read the csv file in NSArray NSArrays and go backwards is pretty trivial.

It will be something like this (warning: code entered in the browser):

NSArray * data = ...; //An NSArray of NSArrays of NSStrings NSMutableString * csv = [NSMutableString string]; for (NSArray * line in data) { NSMutableArray * formattedLine = [NSMutableArray array]; for (NSString * field in line) { BOOL shouldQuote = NO; NSRange r = [field rangeOfString:@","]; //fields that contain a , must be quoted if (r.location != NSNotFound) { shouldQuote = YES; } r = [field rangeOfString:@"\""]; //fields that contain a " must have them escaped to "" and be quoted if (r.location != NSNotFound) { field = [field stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]; shouldQuote = YES; } if (shouldQuote == YES) { [formattedLine addObject:[NSString stringWithFormat:@"\"%@\"", field]]; } else { [formattedLine addObject:field]; } } NSString * combinedLine = [formattedLine componentsJoinedByString:@","]; [csv appendFormat:@"%@\n", combinedLine]; } [csv writeToFile:@"/path/to/file.csv" atomically:NO]; 
+4
source

The general solution is to use stringWithFormat: to format each line. Presumably you are writing this to a file or socket, in which case you should write a data representation of each line (see dataUsingEncoding: in the file descriptor when you create it.

If you format many strings, you can use the initWithFormat: and explicit release messages to avoid running out of memory by piling up too many string objects in the autocomplete pool.

And always, always, always remember to avoid the correct values before passing them to the formatting method.

Escalation (along with cancellation) is a really good thing to write unit tests. Write the function in CSV format on one line and check the test cases that compare its result in order to correct the output. If you have a CSV parser, or you need it, or just want to make sure your escaping is correct, write unit tests to parse and undo, as well as escaping and formatting.

If you can start with a single record containing any combination of CSV special characters and / or SQL characters, format it, parse the formatted string and end up writing a record equal to the one you started with, you know your code is good.

(All of the above apply equally to CSV and HTML. If possible, you can use XHTML so you can use XML validation and analysis tools, including NSXMLParser.)

+2
source

CSV - Comma Separated Values

I usually just iterate over the data structures in my application and output one set of values ​​per line, the values ​​inside the set are separated by a comma.

 struct person { string first_name; string second_name; }; person tony = {"tony", "momo"}; person john = {"john", "smith"}; 

will look like

 tony, momo john, smith 
0
source

Source: https://habr.com/ru/post/1309726/


All Articles