Can NSProxy stand in for NSView subclasses?

2.6k Views Asked by At

I've tried a simple test which results in EXC_BAD_ACCESS error:

#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

Is there something wrong with this test, or is there a fundamental reason why NSView subclasses cannot be used with NSProxy?

Error details:

Thread 1: EXC_BAD_ACCESS (code=1, address=0xc) at [NSView addSubview:]

0

There are 0 best solutions below