2

Do I have this right ...

To manipulate files on disk (create, copy, rename etc.) you use NSFileManager

To manipulate file contents (open, read, close etc.) you use NSFileHandle

I just want to make sure I am understanding this right.

EDIT_001

Thanks, thats what I figured Joshua, so I am assuming that by using the example below, open and close are both handled automatically by the implementation.

fileContents = [NSString stringWithContentsOfFile:fileOnDisk
                         encoding:NSMacOSRomanStringEncoding 
                         error:&fileError];

gary

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • Can you please check this: https://stackoverflow.com/questions/52944065/handle-writedata-method-on-nsfilehandle-on-main-thread?noredirect=1#comment92796308_52944065 – nr5 Oct 23 '18 at 10:09

2 Answers2

3

More or less, yes. From the docs:

NSFileHandle objects provide an object-oriented wrapper for accessing open files or communications channels.

... though NSFileHandle isn't necessary to read/write files. You can write an NSString to / read from a file with one line of code and no handle. Depends on what you want to do.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • In answer to your edit, yes, all direct file handling concerns are abstracted away by -stringWithContentsOfFile:encoding:error: – Joshua Nozzi Dec 14 '09 at 20:19
  • You need only be concerned with whether the encoding is correct and whether the method fails (returns nil) and has produced an associated error. – Joshua Nozzi Dec 14 '09 at 20:20
  • can you please check this one: https://stackoverflow.com/questions/52944065/handle-writedata-method-on-nsfilehandle-on-main-thread?noredirect=1#comment92796308_52944065 – nr5 Oct 23 '18 at 10:01
0

I believe, for the most part, that NSFileHandle is a wrapper around file descriptors -- good for 'relatively' low level reading or writing.

If you are just pushing the contents of a string or NSData to or from a file the associated methods on those classes are hard to beat.

loghound
  • 676
  • 6
  • 12