如何在动态属性中使用typedef?
这是我第一次尝试使用 typedef。诚然,我对发生的情况不太清楚,但我的理解是 typedef 中的值被分配以 0 开头的整数。我尝试将它们用作整数,但收到各种警告和错误。其中之一是“[NSCFNumber objectForKey:]:无法识别的选择器发送到实例”。我不知道如何解决这个问题。我也没有写太多动态 getter/setter,所以我的方法可能是错误的。请帮忙。
// MyView.h
typedef enum
{
STYLE_A,
STYLE_B,
STYLE_C,
STYLE_D
} MyShapeStyle;
@interface MyView : UIView
{
MyShapeStyle shapeStyle;
CALayer *myLayer;
MyLayerDelegate *myLayerDelegate;
}
@property (nonatomic) MyShapeStyle shapeStyle;
@property (nonatomic, retain) CALayer *myLayer;
@property (nonatomic, retain) MyLayerDelegate *myLayerDelegate;
@end
// MyView.m
#import "MyView.h"
@implementation MyView
@dynamic shapeStyle;
@synthesize myLayer;
@synthesize myLayerDelegate;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// Initialization code
MyLayerDelegate *delegate = [[MyLayerDelegate alloc] init];
self.myLayerDelegate = delegate;
CALayer *myLayer = [CALayer layer];
[myLayer setDelegate:delegate];
[self.layer addSublayer:myLayer];
self.myLayer = myLayer;
self.shapeStyle = STYLE_C;
[delegate release];
}
return self;
}
-(MyShapeStyle)shapeStyle
{
return [[self.myLayer valueForKey:@"style"] integerValue];
}
- (void)setShapeStyle:(MyShapeStyle)style
{
[self.myLayer setValue:[NSNumber numberWithInt:style] forKey:@"style"];
}
// MyLayerDelegate.m
-(void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext
{
int id = [[theLayer valueForKey:@"style"] integerValue];
if( id == STYLE_A )
{
}else if ( id == STYLE_B ){
}
}
It's the first time I'm trying to use typedef. Admittedly I don't have a very clear idea of what's going on but my understanding was that the values inside typedef get assigned integers starting with 0. I've tried to use them as integers but I get various warnings and errors. One of them is "[NSCFNumber objectForKey:]: unrecognized selector sent to instance". I don't know how to troubleshoot this. I also haven't written dynamic getters/setters much, so my approach might be wrong. Please help.
// MyView.h
typedef enum
{
STYLE_A,
STYLE_B,
STYLE_C,
STYLE_D
} MyShapeStyle;
@interface MyView : UIView
{
MyShapeStyle shapeStyle;
CALayer *myLayer;
MyLayerDelegate *myLayerDelegate;
}
@property (nonatomic) MyShapeStyle shapeStyle;
@property (nonatomic, retain) CALayer *myLayer;
@property (nonatomic, retain) MyLayerDelegate *myLayerDelegate;
@end
// MyView.m
#import "MyView.h"
@implementation MyView
@dynamic shapeStyle;
@synthesize myLayer;
@synthesize myLayerDelegate;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// Initialization code
MyLayerDelegate *delegate = [[MyLayerDelegate alloc] init];
self.myLayerDelegate = delegate;
CALayer *myLayer = [CALayer layer];
[myLayer setDelegate:delegate];
[self.layer addSublayer:myLayer];
self.myLayer = myLayer;
self.shapeStyle = STYLE_C;
[delegate release];
}
return self;
}
-(MyShapeStyle)shapeStyle
{
return [[self.myLayer valueForKey:@"style"] integerValue];
}
- (void)setShapeStyle:(MyShapeStyle)style
{
[self.myLayer setValue:[NSNumber numberWithInt:style] forKey:@"style"];
}
// MyLayerDelegate.m
-(void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext
{
int id = [[theLayer valueForKey:@"style"] integerValue];
if( id == STYLE_A )
{
}else if ( id == STYLE_B ){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有理由在该代码中使用
valueForKey:
;只需直接获取/设置各种属性即可。该代码中也不需要
@dynamic
。仅当您要动态生成方法时才需要这样做。至于为什么
objectForKey:
does-not-respond 错误,该代码中没有任何内容应该触发该错误。可能是保留/释放问题,也可能是您未显示的其他代码中的问题。There is no reason to use
valueForKey:
in that code; just get/set the various properties directly.There is also no need for the
@dynamic
in that code. That is only needed if you are going to dynamically generate the methods.As for why the
objectForKey:
does-not-respond error, there isn't anything in that code that should trigger that. Could be a retain/release issue or it could be a problem in some other code that you haven't shown.