有这个 XCTestCase 案例:
- (void)testAllInitializersConfigureTheView {
BIStationAnnotationView *withFrame = [[BIStationAnnotationView alloc] initWithFrame:CGRectNull];
XCTAssertTrue(CGRectEqualToRect(withFrame.frame, CGRectMake(0.f, 0.f, 30.f, 40.f)), @"Frame should be fixed");
}
测试 MKAnnotationView 的子类:
- (id)init {
if (self = [super init]) {
self.frame = _myFrame;
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
return self = [self init];
}
- (instancetype)initWithCoder:(NSCoder *)coder {
return self = [self init];
}
我得到了一个无限循环,因为 initWithFrame
调用了 init
而 init
调用了 initWithFrame
。
谁能解释一下为什么?
最佳答案
我猜 [UIView init]
正在调用 [self initWithFrame:CGRectZero]
,所以它正在调用您自己的 initWithFrame
方法,因为您有重载了它。
要解决你的问题,你应该简单地做同样的事情:)。
init
应该调用 initWithFrame
而不是相反。
关于ios - 继承 MKAnnotationView 并从 initWithFrame : overloads 调用 init 时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613428/