0

我尝试了一个导致 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 (code=1, address=0xc) at [NSView addSubview:]

4

0 回答 0