Property not found on object of type "PNREmployee"


#1

Hi guys, i have a problem with class extension. When i’m creating a class extension, the message of Xcode is: Property ‘test’ not found on object of type “PNREmployee”

I’m writing a simple example for understanding:

BRNEmployee.h

#import <Foundation/Foundation.h>

@interface PRNEmployee : NSObject
@property (nonatomic) unsigned int *ID;

@end

BNREmployee.m

#import "PRNEmployee.h"

@interface PRNEmployee()
@property (nonatomic) unsigned int age;
@end

@implementation PRNEmployee

@end

main.m

#import <Foundation/Foundation.h>
#import "PRNEstensione.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        PRNEmployee *test = [[PRNEmployee alloc] init];
        
        test.age = 18;                  //Property 'age' not found on object of type "PNREmployee"

    }
    return 0;
}

I use Xcode 6.1.1
Why have i this problem?


#2

[quote]

[code]#import “PRNEmployee.h”

@interface PRNEmployee()
@property (nonatomic) unsigned int age;
@end

@implementation PRNEmployee

@end[/code]
…Why have i this problem?[/quote]
That’s because you are using an anonymous, private category.

Make your category public by giving it a name and creating its own files.

PRNEmployee+Extension.h

#import "PRNEmployee.h"

@interface PRNEmployee (Extension)
@property (nonatomic) unsigned int age;
@end

PRNEmployee+Extension.m

#import "PRNEmployee+Extension.h"

@implementation PRNEmployee (Extension)

- (unsigned int)age {
    return _age;
}

- (void)setAge:(unsigned int)age {
    _age = age;
}
@end

PRNEmployee.h

#import <Foundation/Foundation.h>

@interface PRNEmployee : NSObject {
    unsigned int _age;
}
@property (nonatomic) unsigned int * ID;
@end

PRNEmployee.m

#import "PRNEmployee.h"

@implementation PRNEmployee
@end

main.m

#import "PRNEmployee+Extension.h"

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        
        PRNEmployee *test = [[PRNEmployee alloc] init];
        
        test.age = 18;
        printf ("%u\n", test.age);
    }
    return 0;
}

[Become a competent programmer: pretty-function.org]


#3

Thank you for your reply. I want to create an anonymous class o hide instance variables and methods. I followed the instructions written in the book. They say to write the section @interface in () within implementation.m. When I run the program it doesn’t recognise what I’ve written in the anonymous class.