我正在尝试扩展标准的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;

但是,我无法使“_”正常工作。

编译器抛出一个en错误“_depth not found”!

最佳答案

这是您应该怎么做:

在实现文件导入目标运行时中-仅包括

#import <objc/runtime.h>

在下面添加静态变量
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);
}

对类别中添加的所有属性应用类似的解决方案

关于ios - EXC_BAD_ACCESS尝试扩展UIView类时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24417470/

10-12 00:18
查看更多