Bartosz Kunat Bartosz Kunat - 3 months ago 13
iOS Question

Objective-C Class extensions

I've started learning obj-C just recently so sorry if I'm missing something obvious.
I want to access "passCode" (extension) with clpPlayerStats object, but it's impossible to do ("Property not found on object..."). Is there any simple way to fix this?

clpPlayerStats.h

#import <Foundation/Foundation.h>

@interface clpPlayerStats : NSObject

@property(nonatomic, copy) NSString* name;

@end


clpPlayerStats.m

#import "clpPlayerStats.h"

@interface clpPlayerStats()

@property (nonatomic) unsigned int passCode;

@end

@implementation clpPlayerStats
@end


main.m

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

int main(int argc, const char * argv[]) {
@autoreleasepool {
clpPlayerStats *clapslock = [[clpPlayerStats alloc] init];
NSString *username = [NSString stringWithFormat:@"xxxPussySlayerxxx"];
clapslock.name = username;
clapslock.passCode = 12; // <------ "Property not found on object..."
}
return 0;
}

Answer

With this code:

@interface clpPlayerStats()

    @property (nonatomic) unsigned int passCode;

@end

you are declaring a private property. If you want to use passCode outside the scope of your class you have to add in the public interface:

#import <Foundation/Foundation.h>

@interface clpPlayerStats : NSObject

    @property(nonatomic, copy) NSString* name;
    @property (nonatomic) unsigned int passCode; 

@end

and remove the interface in the .m

Comments