gpt4 book ai didi

ios - 继承 MKAnnotationView 并从 initWithFrame : overloads 调用 init 时无限循环

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:12 27 4
gpt4 key购买 nike

有这个 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 调用了 initinit 调用了 initWithFrame

enter image description here

谁能解释一下为什么?

最佳答案

我猜 [UIView init] 正在调用 [self initWithFrame:CGRectZero],所以它正在调用您自己的 initWithFrame 方法,因为您有重载了它。

要解决你的问题,你应该简单地做同样的事情:)。init 应该调用 initWithFrame 而不是相反。

关于ios - 继承 MKAnnotationView 并从 initWithFrame : overloads 调用 init 时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com