Re: protocol and properties
Re: protocol and properties
- Subject: Re: protocol and properties
- From: "Stephen J. Butler" <email@hidden>
- Date: Sun, 28 Nov 2010 02:50:25 -0600
On Sun, Nov 28, 2010 at 2:15 AM, Martin Hewitson
<email@hidden> wrote:
> Suppose I have a formal protocol which defines a method:
>
> - (NSArray*)objects;
>
> Then I implement a class which implements this protocol. To do that I make a property:
>
> @property (nonatomic, readwrite, retain) NSArray * objects;
>
> and put the appropriate synthesize statement in the implementation.
>
> I get compiler warnings that this class doesn't implement the protocol. It seems it doesn't take the synthesized getter as being an implementation of the -objects method.
>
> I also tried explicitly adding the implementation, but the warning remains:
>
> - (NSArray*) objects
> {
> return objects;
> }
>
> Am I doing something wrong here, or is it not possible to use a property to satisfy a protocol?
Works for me:
#import <Foundation/Foundation.h>
@protocol Bar
- (NSArray*) objects;
@end
@interface Foo : NSObject < Bar > {
NSArray *objects;
}
@property (nonatomic, readwrite, retain) NSArray *objects;
@end
@implementation Foo
#if 0
- (NSArray*) objects { return objects; }
- (void) setObjects:(NSArray*)o {
[o retain];
[objects autorelease];
objects = o;
}
#else
@synthesize objects;
#endif
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo *f = [[[Foo alloc] init] autorelease];
id< Bar > b = f;
f.objects = [NSArray arrayWithObjects:@"one",
@"two", @"three", nil];
NSLog( @"objects = %@", [b objects] );
[pool drain];
return 0;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden