1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Saving problems.

Discussion in 'Mac Programming' started by BlakTornado, Feb 1, 2008.

  1. Guest

    BlakTornado

    #1
    Well I was really inspired to make an app after firstly hearing about some Mac Indie Developer tales and downloading some Safari plug-ins, so I decided to install Xcode.

    I've made a basic little Word Processor that I planned to use instead of TextEdit as I can't afford iWork, yet want something less depressing than TextEdit (It's quite a dull app, really xD), so I set off on making an app by following tutorials online.

    See, I'm a Movie Maker and musician who so happened to start web development to promote my stuff. This led me to use Leopard's Dashcode to make some widgets - which was an enjoyable experience to say the least (Particularly because I now know a heck of a lot about HTML, Javascript, etc.)... and after realizing how easy Apple had made it to develop widgets, I thought I'd give Software Development a go. Nothing huge but this is just early days (I do have some crazily ambitious plans for this small text editor I'm working on, I must say :p)

    But before the exciting, innovative features can come along, I must get the bear essentials working first... and right now, I'm stuck on saving.

    I followed the instructions on Apple's site but can't make heads nor tales of it.

    Basically, I have this:

    Code:
    #import "MyDocument.h"
    
    @implementation MyDocument
    
    - (id)init
    {
        self = [super init];
        if (self) {
        
            // Add your subclass-specific initialization here.
            // If an error occurs here, send a [self release] message and return nil.
        
        }
        return self;
    }
    
    - (NSString *)windowNibName
    {
        // Override returning the nib file name of the document
        // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
        return @"MyDocument";
    }
    
    - (void)windowControllerDidLoadNib:(NSWindowController *) aController
    {
        [super windowControllerDidLoadNib:aController];
        // Add any code here that needs to be executed once the windowController has loaded the document's window.
    }
    
    - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
    {
        // Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
    
        // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    
        // For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
    
        if ( outError != NULL ) {
    		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
    	}
    	return nil;
    }
    
    - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
    {
        // Insert code here to read your document from the given data of the specified type.  If the given outError != NULL, ensure that you set *outError when returning NO.
    
        // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. 
        
        // For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
        
        if ( outError != NULL ) {
    		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
    	}
        return YES;
    }
    
    [B]const OSType kMyAppCreatorCode = 'ttxt';
    
    - (NSDictionary *)fileAttributesToWriteToURL:(NSURL *)absoluteURL
        ofType:(NSString *)typeName
        forSaveOperation:(NSSaveOperationType)saveOperation
        originalContentsURL:(NSURL *)absoluteOriginalContentsURL
        error:(NSError **)outError
    {
        NSMutableDictionary *fileAttributes =
                [[super fileAttributesToWriteToURL:absoluteURL
                 ofType:typeName forSaveOperation:saveOperation
                 originalContentsURL:absoluteOriginalContentsURL
                 error:outError] mutableCopy];
        [fileAttributes setObject:[NSNumber numberWithUnsignedInt:'kMyAppCreatorCode']
            forKey:NSFileHFSCreatorCode];
        [fileAttributes setObject:[NSNumber numberWithUnsignedInt:'kMyDocumentTypeCode']
            forKey:NSFileHFSTypeCode];
        return [fileAttributes autorelease];
    }[/B]
    
    @end
    
    That's my file called MyDocument.m which I am assuming is the same as "NSDocument" since that's what worked with the file types... I think (and nothing comes up when I search NSDocument in Xcode).

    Xcode says that "[fileAttributes setObject:[NSNumber numberWithUnsignedInt:'kMyAppCreatorCode']" (in both places) is "Character constant too long for it's type" - and I have no idea what that means. Whatever it is, it means I can't save files, that's for sure.

    All help appreciated and I'll put your name in the App credits.
     
  2. Moderator emeritus

    kainjow

    #2
    How long have you been learning Cocoa? You can't just pick something up like this after knowing HTML and think you'll get it :). People go to college for this type of stuff. If you want to write a text editor and have no prior experience with software development (HTML and JavaScript don't count), pick up a book on Cocoa.
     
  3. macrumors 601

    HiRez

    #3
    I agree you need to go through and methodically learn Cocoa (and at least some Objective-C) first. There's a lot you can do by guesswork, and by examining sample code, but you will become frustrated if you proceed without a foundation to work on. The good news is it's not that hard if you put a little time into it and there are some good resources for it, both online and in print. Search on these forums because the resources question has been answered 1,000 times.

    For starters though, it looks like you're passing a string into the method instead of an number (your variable name is enclosed in quotes).
     

Share This Page