0

I noticed that when saving a file from within an iOS App using:
writeToFile:myFile atomically:YES

the resulting file is actually saved in XML format, and always has the following code:

<?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>Sentence 1</string>
    <string>Sentence 2</string>
    <string>Sentence 3</string>
    <string>Sentence 4</string>
</array>
</plist>

So its always in an XML/Plist format.

But if I try to import into the App a text file which was NOT created from within an iOS App - it doesn't seem to know how to recognize it.

For example, if I take a plain text file created in TextEdit and drag it into my App's Documents folder and then run the App again, the App sees this new file, but when I try to load its contents using: initWithContentsOfFile:myFile
the result I get is: (null)

Is this because the default format of files created in TextEdit is ".rtf"? Or should I be using an altogether different method to read plain-text files into the App other than initWithContentsOfFile:myFile?

Ultimately I'd like for the App to be able to load existing text files - maybe even Microsoft WORD files - and work with them. I'd like for the user to drag their existing files into the App's folder in iTunes, and then sync their device with iTunes - which will then copy these new files onto the device and when they run their App again, the app will see the new files and be able to open them and read their contents.

Any suggestions?

sirab333
  • 3,662
  • 8
  • 41
  • 54

1 Answers1

1

You've mentioned the writeToFile:atomically: method but you haven't told us what class this method is being called on. For example, NSString, NSArray, NSDictionary and other classes have a method with this name and they all behave differently as might expect given teh different types of data structures.

From the above file output, it looks like you're probably calling this on an NSArray which is why it gets written in Plist format. I assure you that strings get written plainly if you using an NSString, eg:

NSString *s = @"A string";
[s writeToFile:path atomically:YES];

will produce a file with:

A String

You may want to convert various other data structures into a string before you write it to a file.

Marplesoft
  • 6,030
  • 4
  • 38
  • 46
  • OK, I didn't know other classes also had a `writeToFile:atomically:` method - or an `initWithContentsOfFile` method. Good to know:-) And you're right: I was using NSArray's save method. Just now I tried reading a plain-jane text file created in TextEdit using NSString's `initWithContentsOfFile` method - and it worked (meaning I didn't get '(null)') - so that's great. Next I tried reading an *RTF* file created with TextEdit and that also worked, except I got a lot of junk characters like: `{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf390` - any idea how to get rid of those? – sirab333 Aug 07 '13 at 01:00
  • Those aren't junk characters - those are RTF codes. See http://en.wikipedia.org/wiki/Rich_Text_Format – Marplesoft Aug 07 '13 at 17:04
  • yes I know they're not junk - I know what RTF is - which is why I quite specifically tried to test-load such a file into the App in the first place, to see how/if it would handle all its formatting code/characters ("junk" :-)) -- which leads me to repeat the question: do you know how to strip those characters away or somehow convert an rtf file to plain text file - on the fly, during runtime? Is it even possible? – sirab333 Aug 09 '13 at 00:06
  • Sorry, I don't. Probably best to post that as a different question. – Marplesoft Aug 09 '13 at 01:03