Resolved Important Newbie Questions

Discussion in 'Mac Programming' started by Richard Birkett, Aug 21, 2011.

  1. Richard Birkett, Aug 21, 2011
    Last edited: Aug 21, 2011

    Richard Birkett macrumors member

    Joined:
    Aug 21, 2011
    #1
    2 Questions, one about a problem with my app now and another unimportant one:

    1)
    I have a document based application with 2 classes:

    Code:
    
    #import <Cocoa/Cocoa.h>
    
    @interface LPDocument : NSDocument {
    	NSMutableArray *lessonArray;
    }
    
    @property(retain) NSMutableArray *lessonArray;
    
    -(IBAction)createNewLesson:(id)sender;
    
    @end
    
    #import "LPDocument.h"
    
    @implementation LPDocument
    
    @synthesize lessonArray;
    
    - (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 @"LPDocument";
    }
    
    - (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 outError != NULL, ensure that you create and set an appropriate error when returning nil.
    	You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    	*/
    	if (outError) {
    	    *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 outError != NULL, ensure that you create and set an appropriate error when returning NO.
    	You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    	If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    	*/
    	if (outError) {
    	    *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
    	}
    	return YES;
    }
    
    + (BOOL)autosavesInPlace
    {
        return YES;
    }
    
    -(void)createNewLesson:(id)sender
    {
    	Lesson *newLesson;
    		//newLesson [Lesson lessonWith];
    }
    
    @end
    
    #import <Cocoa/Cocoa.h>
    #import <Foundation/Foundation.h>
    
    @interface Lesson : NSObject {
    	NSString *title;
    	NSDate *creationDate;
    	NSArray *keyPoints;
    	NSTextStorage *notes;
    }
    
    @property(retain) NSString *title;
    @property(retain, readonly) NSDate *creationDate;
    @property(retain) NSArray *keyPoints;
    @property(retain) NSTextStorage *notes;
    
    + (Lesson *)lessonWithTitle:(NSString *)newTitle keyPoints:(NSArray *)newKeyPoints notes:(NSTextStorage *)newNotes;
    
    @end#import "Lesson.h"
    
    @implementation Lesson
    
    @synthesize title, creationDate, keyPoints, notes;
    
    + (Lesson *)lessonWithTitle:(NSString *)newTitle
    				  keyPoints:(NSArray *)newKeyPoints
    					  notes:(NSTextStorage *)newNotes
    {
    	Lesson *newLesson = [[[self alloc] init] autorelease];
    	newLesson.title = newTitle;
    	newLesson.keyPoints = newKeyPoints;
    	newLesson.notes = newNotes;
    	return newLesson;
    }
    
    - (id)init
    {
        self = [super init];
        if (self) {
            creationDate = [NSDate date];
        }
        
        return self;
    }
    
    @end
    
    My problem is at the end of the first class' implementation, it has two 'semantic' errors for this line (which used to assign before I reduced it to a variable declaration)

    Lesson *newLesson; //Error 1 is "use of undeclared identifier 'Lesson' ", 2 is "use of undeclared identifier 'newLesson' "

    I don't understand why because Lesson is a class name and is not an iVar. Help! :)

    2)
    What the the header file @interface method declarations do? Why are they important? I ask because the documentation says it is necessary but when I create a document based app (as shown above) it sets up methods in @implementation with declaring them in the header, it also seems to work.

    Thanks in advance for replies or even a solution! :D
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    In LPDocument.h, add #import "Lesson.h"

    You put methods in your interface section in your .h file so others that only have a compiled copy of your class an the .h file know what methods your class supports. This also lets the compiler know what your class responds to at compile time. You can still use methods that are defined in the implementation and not the interface, but generally those are considered "private" and subject to change.

    -Lee
     
  3. Richard Birkett thread starter macrumors member

    Joined:
    Aug 21, 2011
    #3
    Thank you so much! Importing worked and I understand now, NSDocument is quite contained is some ways so it doesn't need to declare much because other classes won't be using the undeclared methods, although I guess I'd better watch that I do declare what the controllers use.
     

Share This Page