I am writing a document based application on OS X. One feature oft this application will be an “import” function.
This function will read data files from the disk which contain data as raw BLOBs and can be parsed into one oder more documents. For this function I will have to create documents programatically. The main problem is, that NSDocumentController only has the makeDocumentWithContentsOfURL:ofType:error: method for creating a document from existing data. There are several way to create my documents and I’d like to know which one would be the cleanest way.
- I could write the data for the documents into separate files on the disk and call
makeDocumentWithContentsOfURL:ofType:error: for each of them. This seems rather inelegant and could slow the app down with large amounts of data. - I could create empty documents with the
NSDocumentControllerand fill the data in. - I could create the documents in the import function and add them to the
NSDocumentControllerwith theaddDocument:method. I don’t know if this will work because this method seems to be a hook for subclassing. - I could subclass
NSDocumentControlleran add amakeDocumentFromData:ofType:error:method. I think this would be clean but more complicated.
What do you think?
Best Regards,
Chris
If you’re targeting Tiger or later, why not use the
-[NSDocument readFromData:ofType:error]method and then invoke-[NSDocumentController addDocument:]? Then you’re good to go with no subclassing involved 🙂To create the data for each object you could get a pointer to the bytes using
-[NSData bytes], some pointer arithmetic, and-[NSData initWithBytesNoCopy:length:]and that avoids the need to create temporary files.