Re: protocol and properties

2010-11-28 Thread Stephen J. Butler
On Sun, Nov 28, 2010 at 2:15 AM, Martin Hewitson
martin.hewit...@aei.mpg.de 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: protocol and properties

2010-11-28 Thread Martin Hewitson
It 'works' for me too, I just warnings from Xcode. Anyway, I'll check in case I 
made a typo or something.

Thanks!

Martin

On 28, Nov, 2010, at 9:50 AM, Stephen J. Butler wrote:

 On Sun, Nov 28, 2010 at 2:15 AM, Martin Hewitson
 martin.hewit...@aei.mpg.de 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;
 }


Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer 
Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: martin.hewit...@aei.mpg.de
WWW: http://www.aei.mpg.de/~hewitson






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


RE: protocol and properties

2010-11-28 Thread Mario Kušnjer

   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?

Yes, you are doing something wrong.
Here you are just using a method that you declared and implemented in a
protocol.
You need to tell a class where that method is declared and implemented, and
you do that like this:

@interface ClassIWantToUseSomeProtocolMethodIn : NSObject
MyProtocolThatIWantToImplement

Bye

Mario Kušnjer

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: protocol and properties

2010-11-28 Thread Stephen J. Butler
On Sun, Nov 28, 2010 at 3:15 AM, Martin Hewitson
martin.hewit...@aei.mpg.de wrote:
 It 'works' for me too, I just warnings from Xcode. Anyway, I'll check in case 
 I made a typo or something.

I don't get any warnings.


 On 28, Nov, 2010, at 9:50 AM, Stephen J. Butler wrote:

 On Sun, Nov 28, 2010 at 2:15 AM, Martin Hewitson
 martin.hewit...@aei.mpg.de 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;
 }

 
 Martin Hewitson
 Albert-Einstein-Institut
 Max-Planck-Institut fuer
    Gravitationsphysik und Universitaet Hannover
 Callinstr. 38, 30167 Hannover, Germany
 Tel: +49-511-762-17121, Fax: +49-511-762-5861
 E-Mail: martin.hewit...@aei.mpg.de
 WWW: http://www.aei.mpg.de/~hewitson
 






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: protocol and properties

2010-11-28 Thread Martin Hewitson
OK, then it seems I didn't do something fundamentally wrong. Probably a stupid 
mistake somewhere.

Thanks again!

Martin

On 28, Nov, 2010, at 10:26 AM, Stephen J. Butler wrote:

 On Sun, Nov 28, 2010 at 3:15 AM, Martin Hewitson
 martin.hewit...@aei.mpg.de wrote:
 It 'works' for me too, I just warnings from Xcode. Anyway, I'll check in 
 case I made a typo or something.
 
 I don't get any warnings.
 
 
 On 28, Nov, 2010, at 9:50 AM, Stephen J. Butler wrote:
 
 On Sun, Nov 28, 2010 at 2:15 AM, Martin Hewitson
 martin.hewit...@aei.mpg.de 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;
 }
 
 
 Martin Hewitson
 Albert-Einstein-Institut
 Max-Planck-Institut fuer
Gravitationsphysik und Universitaet Hannover
 Callinstr. 38, 30167 Hannover, Germany
 Tel: +49-511-762-17121, Fax: +49-511-762-5861
 E-Mail: martin.hewit...@aei.mpg.de
 WWW: http://www.aei.mpg.de/~hewitson
 
 
 
 
 
 
 


Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer 
Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: martin.hewit...@aei.mpg.de
WWW: http://www.aei.mpg.de/~hewitson






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: protocol and properties

2010-11-28 Thread Martin Hewitson

On 28, Nov, 2010, at 10:22 AM, Mario Kušnjer wrote:

 
  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?
 
 Yes, you are doing something wrong.
 Here you are just using a method that you declared and implemented in a
 protocol.
 You need to tell a class where that method is declared and implemented, and
 you do that like this:
 
 @interface ClassIWantToUseSomeProtocolMethodIn : NSObject
 MyProtocolThatIWantToImplement
 

Sorry, I am doing that, I just didn't write it in the mail. That's why I get 
the warnings 'Class doesn't fully implement protocol'. 

Cheers,

Martin

 Bye
 
 Mario Kušnjer
 


Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer 
Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: martin.hewit...@aei.mpg.de
WWW: http://www.aei.mpg.de/~hewitson






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com