Skip to content

Instantly share code, notes, and snippets.

@rustle
Last active December 11, 2015 06:38
Show Gist options
  • Save rustle/4560375 to your computer and use it in GitHub Desktop.
Save rustle/4560375 to your computer and use it in GitHub Desktop.

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;
}
@pgor
Copy link

pgor commented Jan 17, 2013

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;"

@rustle
Copy link
Author

rustle commented Jan 31, 2013

My point was that providing _foo in some methods and not others is not a sensible or intuitive implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment