Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange objective C bug, properties not case sensitive?

Tags:

objective-c

I have two properties "M" and "m", not the best coding style I know but bear with me. Assignment to these properties in the init method does not function properly. Here's the code in it's entirety:

#import "AppDelegate.h"

@interface Foo : NSObject
@property (nonatomic, assign) int M;
@property (nonatomic, assign) int m;
- (id)initWithM:(int)M m:(int)m;
@end

@implementation Foo

- (id)initWithM:(int)M m:(int)m {
    if((self = [super init])) {
        self.M = M;
        printf("M = %d %d\n", M, self.M);
        self.m = m;
        printf("M = %d %d\n", M, self.M);
        printf("m = %d %d\n", m, self.m);
    }
    return self;
}
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    Foo *f = [[Foo alloc] initWithM:2 m:1];
}
@end

And here is the output from the printf's:

M = 2 2
M = 2 1
m = 1 0

If I change "M" to "BAR" and "m" to "bar" it works as I would expect. Is there an explanation for this other than being a compiler bug?

Thanks.

like image 337
Charlie Burns Avatar asked Feb 01 '13 02:02

Charlie Burns


1 Answers

@property int M;
@property int m;

both create

- (void)setM:(int)

If you really wanted to have both an m and an M property (which you definitely shouldn't) you can use

@property int M;
@property (setter = setLowerCaseM:, getter = lowerCaseM)int m;
like image 189
Sebastian Avatar answered Sep 23 '22 02:09

Sebastian