Craig
Craig

Reputation: 1085

How to write an array to a file without html

The code I have right now looks like this:

NSString *path = @"/Users/student/Desktop/conf.txt"

NSArray *myArray = [NSArray arrayWithObjects:@"hello",@"world",@"etc",nil];

[myArray writeToFile:path atomically:YES];

When I look at the text file it writes it ends up looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"     "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>hello</string>
     <string>world</string>
    <string>etc</string>
 </array>
 </plist>

I don't want to display it as an html, I just want a simple text document with the 3 lines saying, "hello world etc".

Does anybody know how I could fix this?

Thanks

Upvotes: 0

Views: 260

Answers (1)

Vladimir
Vladimir

Reputation: 170839

If your array contains strings and you wat to write them to file one per line you can first create string with all your words and then write it to file:

NSString *outString = [myArray componentsJoinedByString:@"\n"];
NSError *error = nil;
[outString writeToFile: path atomically:YES encoding:encoding:NSUTF8Encoding error:&error];

But if you want to serialize your array for future use plist format may be more convenient

Upvotes: 3

Related Questions