views:

51

answers:

3

I have a NSString *csvData which contains the csv data and the format is correct.

In my program (the iPhone), there is a button. When the user click the button, the string will convent to the .csv file. However, I don't know how to convent the NSString to a CSV file. Does the objective C provides any build-in function or class can help me to do?

+1  A: 

See the NSFileManager class reference and especially the - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)contents attributes:(NSDictionary *)attributes.

Best
–f

flohei
@flohei, thank you for your reply. And I want to ask, where does the file store in the iPhone?
Questions
It stores the file at the path you hand over as the first parameter. You can get your documents directory like this:`NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0];`
flohei
@flohei, thank you for your reply. I still have a problem, can you show the steps on how to open the file on the iPhone?
Questions
There are multiple ways. See the `NSString` class reference, for example:http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithContentsOfFile:encoding:error:
flohei
+2  A: 

Read the NSString documentation ? It took me 10seconds :

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error
Benj
@Benj, thank you for your reply. I want to ask where does the file store in the iPhone?
Questions
@MarkSiu wherever you tell it to (as long as you have permission to write there). That's the purpose of the `path` parameter.
Dave DeLong
+1  A: 

To learn more about file paths on the iPhone, read Files and File System in the iOS Application Programming Guide.

johnnieb