Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C changes between OS 2.2.1 and OS 3?

When I tried compiling my app for OS 3 I encountered an the following error:

error: type of accessor does not match the type of property

The error was for a property I tried to access that is defined as follows:

NSMutableArray *myArray

@property (readonly,nonatomic) NSArray* myArray;

the property is @synthesized in the implementation file.

This worked just fine in OS 2.2.1 but doesn't is OS 3.0

Writing the getter method myself solved the problem.

Is anyone aware of changes to objective-c between OS 2.2.1 and 3.0? Is there any documentation for these changes?

The API changes document doesn't appear to contain anything about this issue.

EDIT

the error occurs when you try to access the property e.g.

NSArray *anArray = myClass.myArray;

As I mentioned above I found a workaround for this: writing the getter method myself, however what I'm really after is some kind of documentation from apple explaining this change and any other changes that are not API related.

Thanks for your help

like image 778
Ron Srebro Avatar asked Jun 19 '09 18:06

Ron Srebro


1 Answers

This is a compiler bug.

Though you didn't specify it completely, I expect your code looks like this:

@interface Foo : NSObject {
    NSMutableArray *objects;
}
@property (readonly, copy) NSArray *objects;
@end

@implementation Foo
@synthesize objects;
@end

The compiler is, unfortunately, confused between the declaration of the objects property and the declaration of the objects instance variable. Remember that properties and instance variables are different things in Objective-C; a property can be backed by an instance variable, but it's really part of the public interface of a class.

You can work around this by changing your code to clearly separate the definition of the instance variable from the definition of the property, for example by prefixing the name of the instance variable:

@interface Foo : NSObject {
    NSMutableArray *_objects;
}
@property (readonly, copy) NSArray *objects;
@end

@implementation Foo
@synthesize objects = _objects;
@end

This way the compiler doesn't get confused about the property versus the instance variable in expressions like self.objects (which it shouldn't anyway, but apparently does).

Just to head off the inevitable response: Apple does not reserve the underbar prefix for instance variables. It's reserved for methods. Regardless, if you dislike the underbar, feel free to use another prefix.

like image 81
Chris Hanson Avatar answered Sep 21 '22 13:09

Chris Hanson