Question

Hi i've recently completed a tutorial book on the basics of objective c. And now I'm "Attempting" to make a simple application. Right now I seem to be having the simplest issues which i cannot seem to fix, maybe you could tell me what i'm doing incorrectly.

AppDelegate.h

    #import <Cocoa/Cocoa.h>

    @interface AppDelegate : NSObject <NSApplicationDelegate>

    @property (assign) IBOutlet NSWindow *window;
    - (IBAction)saveData:(id)sender;
    //Below is a simple IBOutlet which I try to retrieve data from when the IBAction saveData occurs
    @property (weak) IBOutlet NSTextField *foodName;


    @end

AppDelegate.m

    #import "AppDelegate.h"

    @implementation AppDelegate

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
    }
    //Here is the saveData IBAction which should print out the value of the NSTextField if the user entered a value (if not returns null)
    - (IBAction)saveData:(id)sender {
        NSLog(@"%@", foodName.stringValue);
        NSLog(@"Saved");
    }
    @end

The problem I seem to have is the build will fail and give me an error message on this line in AppDelegate.m:

    NSLog(@"%@", foodName.stringValue);

the error message is: Use of undeclared identifier 'foodName'; did you mean '_foodName'?

Could someone please explain whats going on and how I can overcome this?

Was it helpful?

Solution

To address the getter method, use self.:

NSLog(@"%@", self.foodName.stringValue);

Also, a minor point: group all your @property declarations together at the start of the @interface statement, but after any instance variable declarations:

@interface MyClass : NSObject {
    NSString *_str1;
    int _i1;
}
@property (weak) IBOutlet NSString *something;
@property (strong, readonly) NSNumber *somethingElse;
- (int)aMethod:(NSString *)string;
- (void)anotherMethod;
@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top