PDA

View Full Version : Some general Cocoa questions about files




subsonix
Jan 26, 2009, 11:13 AM
So I have started to look into objc and Cocoa and have come accross NSFileManager and NSData. Are there any methods to read in just the data part of a file into an NSData object, what I mean is to strip of the header?

And are there any templates for common file formats, so that raw data can be packed and written corectly to these formats?

Also are there a similar level of control over the content of an NSData object as with NSArray?



gnasher729
Jan 26, 2009, 11:31 AM
So I have started to look into objc and Cocoa and have come accross NSFileManager and NSData. Are there any methods to read in just the data part of a file into an NSData object, what I mean is to strip of the header?

And are there any templates for common file formats, so that raw data can be packed and written corectly to these formats?

Also are there a similar level of control over the content of an NSData object as with NSArray?

You use NSFileManager when _you_ determine the exact file format and nobody else. With NSFileManager you just get the raw data (or you could use Posix calls instead, if you want code that runs on other OSes as well).

There are special classes for XML files, classes for Preference files, classes for RTF and HTML, classes for image data and so on.

kainjow
Jan 26, 2009, 12:14 PM
There's also NSFileHandle which might be of interest to you.

subsonix
Jan 26, 2009, 12:26 PM
You use NSFileManager when _you_ determine the exact file format and nobody else. With NSFileManager you just get the raw data (or you could use Posix calls instead, if you want code that runs on other OSes as well).

There are special classes for XML files, classes for Preference files, classes for RTF and HTML, classes for image data and so on.

Hmm, ok so say that I have an application that perform some manipulation on audio file data. Does this mean that I manually will have to check for all possible fileformats that could be used with the program, and set different offsets accordingly? I was hoping that there was a way for this to be handled in the background, since the os allready know this stuff.

subsonix
Jan 26, 2009, 12:29 PM
kainjow, IŽll look into NSFileHandle. Thanks

gnasher729
Jan 26, 2009, 12:44 PM
Hmm, ok so say that I have an application that perform some manipulation on audio file data. Does this mean that I manually will have to check for all possible fileformats that could be used with the program, and set different offsets accordingly? I was hoping that there was a way for this to be handled in the background, since the os allready know this stuff.

No, it doesn't mean that, and that is not what I wrote. If you use NSFileManager, then you have to understand all possible file formats. That's why you wouldn't use NSFileManager, but something more appropriate. For audio files, I would have a good look at the Quicktime or CoreAudio documentation.

It also shows that when you ask questions, you should ask about what you actually want to know, and not about the things that you think you want to know. You wanted to know about handling audio files, but you asked about NSFileManager. Obviously you get answers about NSFileManager, because we are all not very good at distance mind reading.

subsonix
Jan 26, 2009, 01:03 PM
No, it doesn't mean that, and that is not what I wrote. If you use NSFileManager, then you have to understand all possible file formats. That's why you wouldn't use NSFileManager, but something more appropriate. For audio files, I would have a good look at the Quicktime or CoreAudio documentation.

It also shows that when you ask questions, you should ask about what you actually want to know, and not about the things that you think you want to know. You wanted to know about handling audio files, but you asked about NSFileManager. Obviously you get answers about NSFileManager, because we are all not very good at distance mind reading.

To be fair though, the title of the thread is: Some general Cocoa questions about files.

I then continued to say that I have started to look into Cocoa and have come accross NSFileManager and NSData. If there are better methods to achive what I want to know, I want to know, that is the purpose of the thread, a general discussion. So the audio file example served as an example, could have been graphic formats as well.

Catfish_Man
Jan 26, 2009, 01:15 PM
For graphics you'd use NSImage, or CGImage, or ImageIO. The point is that in general you're better off using APIs designed for the type of file you're working with, rather than general file reading APIs.

subsonix
Jan 26, 2009, 01:30 PM
The point is that in general you're better off using APIs designed for the type of file you're working with, rather than general file reading APIs.

Ok, get it. Just starting to look into Cocoa and it helps to get a sense of some of the benefits it presents when dealing with files, as well as how things are organised.

Cheers