views:

88

answers:

1

Hi,

I want to define one protocol with few properties and need to use those properties in another NSObject subclass. Please give me link or example code. I need that to work with 10.5.

Thanks PLEASE CHECK THE FOLLOWING SAMPLE CODE

@protocol MyProtocol
@property (nonatomic, readonly) id someObject;
@property (nonatomic, getter=isAlive) BOOL alive;
@end

#import "MyProtocol.h"
@interface MyCustomClass : NSObject <MyProtocol>{

}
@end

#import "MyCustomClass.h"
@implementation MyCustomClass
@synthesize someObject,alive;

/*
- (id)someObject {
    return nil;
}

- (BOOL)isAlive {
    return YES;
}

- (void)setAlive:(BOOL)aBOOL {
}
*/
@end

**Added: Compling code with x86_64 architecture works fine. But error if i'll change the architecture to i386, then i am getting following warnings:

MyCustomClass.m:13: error: synthesized property 'someObject' must either be named the same as a compatible ivar or must explicitly name an ivar

 error: synthesized property 'alive' must either be named the same as a compatible ivar or must explicitly name an ivar

I just want to know why it is working in x86_64 with @synthesize and not in i386.**

+1  A: 

@property just says to the compiler that the class is expected to define the methods to match that property.

@protocol MyProtocol
@property (nonatomic, readonly) id someObject;
@property (nonatomic, getter=isAlive) BOOL alive;
@end

Anything implementing that protocol will now need to have

- (id)someObject;
- (BOOL)isAlive;
- (void)setAlive:(BOOL)aBOOL;
Joshua Weinberg
Synthesizing the properties should work as well, since it instructs the compiler to supply the accessor method implementations.
Peter Hosey
Thanks, your provided code work perfectly, if i keep the project architecture to x86_64. If i change the architecture to i386 then i am getting following warnings:MyCustomClass.m:13: error: synthesized property 'someObject' must either be named the same as a compatible ivar or must explicitly name an ivar error: synthesized property 'alive' must either be named the same as a compatible ivar or must explicitly name an ivarI just want to know why it is working in x86_64 with @synthesize and not in i386.Thanks
AmitSri
I am trying to write less code if possible. Please also explain me what is the difference between different architectures and which one i suppose to choose to compile the application.
AmitSri
x86-64 uses the new runtime, i386 uses the old one. On the new runtime @synthesize doesn't require a backing ivar for synthesized properties.
Joshua Weinberg
So,Is that means, that i must compile my app with i386 architecture to keep 10.5 compatibility?
AmitSri
10.5 supports 64bit apps. You'd lose support for Intel CoreDuo though. All other intel macs will run 64 bit binaries fine
Joshua Weinberg