Higher level file read/write with Objective-C

936 Views Asked by At

As Objective-C is superset of C, I guess I can use fopen/fread/fwrite/fprint... just like I do with C.

Does that mean that Objective-C doesn't have its unique file processing function?

3

There are 3 best solutions below

2
On BEST ANSWER

Objective-C doesn't have file handling, nor does C. However, the C Standard Library does. And if you want to use Objective-C, you can use the C Standard Library.

Objective-C is mostly used with Foundation, so -[NSData writeToURL:atomically:] and -[NSString writeToURL:atomically:encoding:error:] can be used if you link to the Foundation framework.

For reading files the methods are -[NSData initWithContentsOfURL:options:error:] and -[NSString initWithContentsOfURL:encoding:error:].

0
On

Look at the NSFileManager and NSFileHandle classes for a start. Many other classes, such as NSData, also have methods to read data from a file and write data to a file.

0
On

initWithContentsOfFile:error:

initWithContentsOfURL:error:

writeToFile:atomically:

writeToURL:atomically:

and so on, NSData NSString NSImage and so on all support these methods