Question

another question i'm trying to use a setter within another class but I seem to get this odd error here is the code below:

AppDataSorting.h

    #import <Foundation/Foundation.h>

    @interface AppDataSorting : NSObject{
        NSString *createNewFood;
        NSNumber *createNewFoodCarbCount;
    }

    @property (readwrite) NSString *createNewFood;

    @end

AppDelegate.m

    #import "AppDelegate.h"

    @implementation AppDelegate

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
    }

    - (IBAction)saveData:(id)sender {
        NSLog(@"%@", self.foodName.stringValue);
        self.createNewFood = self.foodName.stringValue;
        NSLog(@"%.1f", self.carbAmount.floatValue);
    }
    @end

I get the error message in AppDelegate.m which is: Property 'createNewFood' not found on object of type 'AppDelegate *'

Could someone please explain the issue here?

Was it helpful?

Solution

You declare this property:

@property (readwrite) NSString *createNewFood;

In AppDataSorting.h so you can access it like self.createNewFood in AppDataSorting.m file not AppDelegate.m. If you want to call it as you do in AppDelegate.m you have move this line:

@property (readwrite) NSString *createNewFood;

to AppDelegate.h file.

Or if you want to use property from AppDataSorting class in AppDelegate you have to create object and call it on that object:

- (IBAction)saveData:(id)sender {
        NSLog(@"%@", self.foodName.stringValue);
        AppDataSorting *dSorting = [[AppDataSorting alloc] init];
        dSorting.createNewFood = self.foodName.stringValue;
        NSLog(@"%.1f", self.carbAmount.floatValue);
    }

OTHER TIPS

In -saveData:, self refers to an instance of NSAppDelegate. The createNewFood property is defined on instances of the class AppDataSorting.

Also note that Cocoa/CF naming conventions give special meaning to methods that start with "init", "new" and (to a lesser degree) "create". You probably want to avoid such things in your property names. Details here.

In general, properties should represent conceptual "properties" of an object. So if you had a Person class, it might have a name property, but it wouldn't have a createNewOutfit property.

You need to access createNewFood on an instance of AppDataSorting - but you're trying to access the property on the AppDelegate-class which clearly doesn't implement it. So you would need to create an instance of AppDataSorting and then access the property like so:

AppDataSorting *instance = [[AppDataSorting alloc] init];
instance.createNewFood = self.foodName.stringValue;

Final notes:

  • The docs provide a good base of information
  • If you don't need atomicity you should always declare properties with the nonatomic attribute
  • createNewFood is not a good name for a property since it suggests a method which creates new food - yet it's only meant to store data (in this case an NSString instance)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top