我有一个带有两个参数的初始化程序:
-(id) initWithSourceFrame:(CGRect)sourceViewFrame mappedFrame:(CGRect)mappedViewFrame {
CGRect copy = mappedViewFrame;
self = [super init];
if (self) {
// not able to access mappedViewFrame here..
// copy is ok
// doing initialization here...
}
return self;
}
它似乎从第二个参数(mappedViewFrame)中得到了错误的值。在查找错误时,我发现 mappedViewFrame 被破坏(在内存中被覆盖?)。这可以在调试器中轻松观察到:
副本仍然保留原始值,因此在这种情况下使用副本是一种解决方法。但我当然想了解为什么会发生这种情况。该类是 NSObject 的直接子类,整个项目是一个 OS X 原生应用程序。第一个论点从未被摧毁。问题与传入的值无关。我切换了它们,它总是第二个被破坏。
例如,我使用这些示例参数调用了初始化程序(与调试器屏幕截图中的不同),错误以相同的方式发生:
Mapper *mapper = [[Mapper alloc] initWithSourceFrame:CGRectMake(0, 0, 100, 100) mappedFrame:CGRectMake(0,0, 200,200)];
方法声明:
-(id) initWithSourceFrame:(CGRect) sourceViewFrame mappedFrame:(CGRect) mappedViewFrame;
我对 Objective-C 有点陌生,所以如果我错过了一些明显的东西,我很抱歉。但是,在方法调用期间参数不保持有效看起来很奇怪。