【发布时间】:2010-07-31 11:27:04
【问题描述】:
我想定义一个具有很少属性的协议,并且需要在另一个 NSObject 子类中使用这些属性。请给我链接或示例代码。我需要它来使用 10.5。
谢谢 请检查以下示例代码
@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
**添加: 使用 x86_64 架构编译代码可以正常工作。但是如果我将架构更改为 i386 则会出错,那么我会收到以下警告:
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
我只想知道为什么它在 x86_64 中使用 @synthesize 而不是在 i386 中工作。**
【问题讨论】:
标签: objective-c cocoa properties protocols