我尝试了一个简单的测试,该测试导致EXC_BAD_ACCESS错误:
#import "AppDelegate.h"
@interface CrashingProxy : NSProxy
@property (strong) id target;
- (instancetype)initWithTarget:(id)targetObj;
@end
@implementation CrashingProxy
@synthesize target;
- (instancetype)initWithTarget:(id)targetObj {
[self setTarget:targetObj];
return self;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:[self target]];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [[self target] methodSignatureForSelector:selector];
}
@end
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(20.0, 20.0, 100.0, 32.0)];
[button setButtonType:NSButtonTypeMomentaryLight];
[button setTitle:@"Hello"];
[[[self window] contentView] addSubview:[[CrashingProxy alloc] initWithTarget:button]];
}
@end
此测试是否有问题,或者是NSView子类不能与NSProxy一起使用的根本原因?
错误详细信息:
线程1:EXC_BAD_ACCESS(代码= 1,地址= 0xc) 在[NSView addSubview:]