【问题标题】:Handling iOS runtime errors处理 iOS 运行时错误
【发布时间】:2016-03-31 08:37:17
【问题描述】:

我已经被移交给了一个 iOS 应用程序的责任(被抛在了深处),而我之前没有任何 iOS 编程经验。当我在阅读 iOS 编程书籍时,我正在尝试使用 Xcode 7 和 OS X El Captain (10.11.1) 中的 SourceTree 运行一个用 ObjectiveC 编写的应用程序。在 Xcode 中,目标设置为 iOS 8。

问题:我已将 GiT 中的源代码从工作版本中提取到我的工作区中,但不幸的是,我什至在进行任何更改之前就遇到了运行时错误!尝试加载视图时代码中断:

- (void)loadView
{
    [super loadView];
    UIView* view = self.view;
    view.backgroundColor = [UIColor colorForBackgroundLight];

    SEFSDashboardHeaderView* headerView = [[SEFSDashboardHeaderView alloc] init];
    self.headerView = headerView;
    [view addSubview:headerView];

    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        UIView* topLayoutGuide = (UIView *)self.topLayoutGuide;
        make.left.equalTo(view);
        make.top.equalTo(topLayoutGuide.mas_bottom);
        make.right.equalTo(view);
        make.height.equalTo(@85);
    }];

    UITableView* tableView = self.tableView;
    tableView.rowHeight = 58;
    tableView.backgroundColor = view.backgroundColor;
    [view addSubview:tableView];
    self.tableView = tableView;

    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view);
        make.right.equalTo(view);
        make.bottom.equalTo(view);
        make.top.equalTo(headerView.mas_bottom);
    }];
}

违规行是:

make.top.equalTo(topLayoutGuide.mas_bottom);

当我在 [topLayoutGuide.mas_bottom] 上执行 po 时,在调试区域中,我得到:

(lldb) po [topLayoutGuide mas_bottom] 错误:警告:无法获取 cmd 指针(替换为 NULL):在此帧中找不到名为“_cmd”的变量 执行被中断,原因:试图取消引用无效的 ObjC 对象或向其发送无法识别的选择器。 该过程已返回到表达式评估之前的状态。 (lldb)

我的理解是topLayoutGuide对象中不存在属性mas_bottom

但是在 massConstraintMaker.m 类中有一个对 mas_bottom 的引用,代码如下:

- (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs {
    MASAttribute anyAttribute = MASAttributeLeft | MASAttributeRight | MASAttributeTop | MASAttributeBottom | MASAttributeLeading | MASAttributeTrailing | MASAttributeWidth | MASAttributeHeight | MASAttributeCenterX | MASAttributeCenterY | MASAttributeBaseline;

    NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)");

    NSMutableArray *attributes = [NSMutableArray array];

    if (attrs & MASAttributeLeft) [attributes addObject:self.view.mas_left];
    if (attrs & MASAttributeRight) [attributes addObject:self.view.mas_right];
    if (attrs & MASAttributeTop) [attributes addObject:self.view.mas_top];
    if (attrs & MASAttributeBottom) [attributes addObject:self.view.mas_bottom];
    if (attrs & MASAttributeLeading) [attributes addObject:self.view.mas_leading];
    if (attrs & MASAttributeTrailing) [attributes addObject:self.view.mas_trailing];
    if (attrs & MASAttributeWidth) [attributes addObject:self.view.mas_width];
    if (attrs & MASAttributeHeight) [attributes addObject:self.view.mas_height];
    if (attrs & MASAttributeCenterX) [attributes addObject:self.view.mas_centerX];
    if (attrs & MASAttributeCenterY) [attributes addObject:self.view.mas_centerY];
    if (attrs & MASAttributeBaseline) [attributes addObject:self.view.mas_baseline];

    NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count];

    for (MASViewAttribute *a in attributes) {
        [children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]];
    }

MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children];
constraint.delegate = self;
[self.constraints addObject:constraint];
return constraint;

}

是否存在配置设置问题?我应该以不同的方式启动应用程序吗?我目前正在打开 .xcworkspace 文件而不是 .xcodeproj。希望这是正确的方法。

如果我至少可以在没有任何运行时错误的情况下运行该应用程序,那将是一份很棒的圣诞礼物!谢谢。

【问题讨论】:

  • 你能告诉我们错误吗?只需从控制台复制错误日志。
  • @Dev_Tandel 我已将错误日志添加到问题中。
  • 你可以从跟踪mas_bottom的值开始,每次调用时设置一个断点,也许在某个时候它会被释放或其他东西。
  • 使用可可豆荚?指定了库的显式发布版本?知道过去使用哪个特定版本吗?

标签: ios objective-c


【解决方案1】:

通过删除 XCODE 7.1 并安装 XCODE 6.4 解决了问题。

【讨论】:

  • 虽然这可能避免了错误,但它并不能解决问题,而且绝对不是正确的答案。简单地降级并不能解决手头的问题。
猜你喜欢
  • 2015-12-06
  • 2020-09-19
  • 2016-03-18
  • 2017-09-05
  • 2014-12-20
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多