Re: writetofile oftype
Re: writetofile oftype
- Subject: Re: writetofile oftype
- From: Phill Kelley <email@hidden>
- Date: Wed, 23 Jul 2003 10:19:59 +1000
At 15:55 -0400 22/07/2003, <email@hidden> wrote:
>
I must be misreading something or not understanding what I am seeing, after
>
all, 99% of all errors are of the type ID10T.
>
In the write to file of type method, I think I understand that the oftype
>
part is where you tell it to use your filetype/creator code.
>
My question...
>
how would one determine the format.meaning.. If I want the file to save in
>
plain text format say with a ".h" extension (Yes i know .h is a header file
>
extension.) or ".txt" extension, how do I pass this information. I've
>
tried looking on the developer.apple.com but cannot seem to place my eyes
>
on the information I need.
Start with:
/Developer/Documentation/Cocoa/
TasksAndConcepts/ProgrammingTopics/
Documents/index.html
Among other things, that will point you in the general direction of the
Info.plist in your app's target. Basically, the "ofType" parameter is an
NSString which identifies the CFBundleTypeName field of one of the entries
in the CFBundleDocumentTypes array in your app's Info.plist.
When you want an example to look at, try /Developer/Examples/AppKit/Sketch.
Click the "Targets" tab, then click the "Sketch" target, then click the
"Document Types" entry under the "Simple View" of the "Info.plist Entries".
What you see in the "Name" column is what will appear in the "ofType"
parameters that the various file-related NSDocument methods are talking
about.
While you're considering that, also click on the "Expert View", then expand
the CFBundleDocumentTypes entry and nose around. You'll observe that
there's a lot more in the expert view than the simple view (keep that in
mind if you're contemplating something complex, such as being able to
export given kind of file but not read it).
What writing a file all boils down to is overriding
dataRepresentationOfType to return an NSData containing your data. If you
have more than one kind of file, then that method needs to do something
like:
if ([aType isEqualToString:MyFirstType]) {
// generate first kind of data
} else if ([aType isEqualToString:MySecondType]) {
// generate second kind of data
} else
return nil;
where MyFirstType and MySecondType are #defined to exactly match the name
fields in your bundle's plist.
If you want to present your user with a choice of file formats, override
shouldRunSavePanelWithAccessoryView to return YES and you'll get a popup in
the save dialog listing all the "name" fields.
All of the above gets the correct file extension attached to the file but
doesn't deal with the HFS creator and type. To accomplish that, also
override:
- (NSDictionary*) fileAttributesToWriteToFile:(NSString *)fullDocumentPath
ofType:(NSString *)documentTypeName
saveOperation:(NSSaveOperationType)saveOperationType
to return a dictionary containing the necessary info. There's an example in
the documentation I mentioned at the beginning of this reply (click on the
last entry under "Tasks").
Regards, Phill
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.