EXC_BAD_ACCESS尝试扩展UIView类时

时间:2014-06-25 19:58:40

标签: ios objective-c uiview

我试图扩展标准UIView类,但我收到了EXC_BAD_ACCCESS错误。

的UIView + Depth.h

#import <UIKit/UIKit.h>

@interface UIView (Depth)
@property (nonatomic, assign) CGSize initialSize;
@property (nonatomic, assign) CGSize angularSize;
@property (nonatomic, assign) CGFloat depth; // 0.0 ... 1.0
@end

的UIView + Depth.m

#import "UIView+Depth.h"

@implementation UIView (Depth)
@dynamic initialSize;
@dynamic angularSize;
@dynamic depth;

- (CGSize)initialSize
{
    return self.initialSize;
}

- (void)setInitialSize:(CGSize)initialSize
{
    self.initialSize = initialSize;
}

- (CGSize)angularSize
{
    return self.angularSize;
}


- (void)setAngularSize:(CGSize)angularSize
{
    self.angularSize = angularSize;
}

- (CGFloat)depth
{
    return self.depth;
}

- (void)setDepth:(CGFloat)depth // This is where I'm getting the error while running.
{
    self.depth = depth;

    if (self.initialSize.width == 0 && self.initialSize.height == 0) {

        self.initialSize = self.frame.size;
    }

    // Angular size
    self.angularSize = CGSizeMake((powf(self.initialSize.width, 2) + powf(self.initialSize.height, 2)) * powf((1 - depth), 2) /
                                  (1 - (powf(self.initialSize.height, 2) / powf(self.initialSize.width, 2))),
                                  (powf(self.initialSize.width, 2) + powf(self.initialSize.height, 2)) * powf((1 - depth), 2) /
                                  (1 - (powf(self.initialSize.width, 2) / powf(self.initialSize.height, 2))));

    // Frame size
    self.frame = CGRectMake(self.center.x - self.angularSize.width / 2,
                            self.center.y - self.angularSize.height / 2,
                            self.angularSize.width,
                            self.angularSize.height);
}

我使用@dynamic属性来实现我自己的setter和getter方法。 有关如何修复它的任何建议吗?

我知道我应该使用:

_depth = depth;

而不是:

self.depth = depth;

但是,我无法得到&#34; _&#34;工作。

编译器抛出一个错误&#34; _depth not found&#34; !

2 个答案:

答案 0 :(得分:2)

这是一种无限循环的方法......

- (void)setDepth:(CGFloat)depth // This is where I'm getting the error while running.
{
    self.depth = depth; //same as [self setDepth:depth];
}

您还有其他问题。在类别中添加ivars并不是那么简单......

http://nshipster.com/associated-objects/

答案 1 :(得分:1)

这是你应该做的:

在实现文件导入目标运行时 - 只需包含

#import <objc/runtime.h>

下面添加静态var

static CGFloat _depth;

使用以下代码替换setter和getter:

<强>吸气剂

-(CGFloat)depth {
    return (CGFloat) [objc_getAssociatedObject(self, &_depth) floatValue];
}

<强>设定器

-(void)setDepth:(CGFloat)depth {
    objc_setAssociatedObject(self, &_depth , [NSNumber numberWithFloat:depth] , OBJC_ASSOCIATION_RETAIN);

    if (self.initialSize.width == 0 && self.initialSize.height == 0) {

        self.initialSize = self.frame.size;
    }

    // Angular size
    self.angularSize = CGSizeMake((powf(self.initialSize.width, 2) + powf(self.initialSize.height, 2)) * powf((1 - depth), 2) /
                                  (1 - (powf(self.initialSize.height, 2) / powf(self.initialSize.width, 2))),
                                  (powf(self.initialSize.width, 2) + powf(self.initialSize.height, 2)) * powf((1 - depth), 2) /
                                  (1 - (powf(self.initialSize.width, 2) / powf(self.initialSize.height, 2))));

    // Frame size
    self.frame = CGRectMake(self.center.x - self.angularSize.width / 2,
                            self.center.y - self.angularSize.height / 2,
                            self.angularSize.width,
                            self.angularSize.height);
}

将类似的解决方案应用于类别

中添加的所有属性