Builds and runs no problem:
@interface Foo : NSObject
@property (readonly) id foo;
@end
@implementation Foo
- (instancetype)init
{
self = [super init];
if (self)
{
_foo = [NSObject new];
}
return self;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Foo *foo = [Foo new];
NSLog(@"%@", foo.foo);
}
return 0;
}
Compiler error:
@interface Foo : NSObject
@property (readonly) id foo;
@end
@implementation Foo
- (instancetype)init
{
self = [super init];
if (self)
{
_foo = [NSObject new];
}
return self;
}
- (id)foo
{
return _foo;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Foo *foo = [Foo new];
NSLog(@"%@", foo.foo);
}
return 0;
}
By implementing the sole accessor on a readonly property, you are assumed to be handling whatever synthesis or indirection you'd want for that property, so autosynthesize is disabled for that property.
To get _foo, add "@synthesize foo = _foo;"