如何在动态属性中使用typedef?

发布于 2024-09-28 04:00:09 字数 1725 浏览 4 评论 0原文

这是我第一次尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2024-10-05 04:00:09

没有理由在该代码中使用 valueForKey:;只需直接获取/设置各种属性即可。

-(MyShapeStyle)shapeStyle
{
    return (MyShapeStyle) self.myLayer.style;
}

该代码中也不需要 @dynamic。仅当您要动态生成方法时才需要这样做。

至于为什么 objectForKey: does-not-respond 错误,该代码中没有任何内容应该触发该错误。可能是保留/释放问题,也可能是您未显示的其他代码中的问题。

There is no reason to use valueForKey: in that code; just get/set the various properties directly.

-(MyShapeStyle)shapeStyle
{
    return (MyShapeStyle) self.myLayer.style;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文