【问题标题】:Getting error due to item found nil during unwrapping of Optional Value由于在展开可选值期间发现项目为零而出现错误
【发布时间】:2020-05-02 19:41:26
【问题描述】:

我正在尝试添加一个按钮,允许用户刷新表格视图并将它们带回表格顶部。我正在尝试添加一个设置了类似 var layoutGuide:UILayoutGuide! 的 var 的代码的 sn-p,但它给了我这个消息。 然后我尝试运行模拟器并简单地忽略此消息,但随后我收到一条错误消息,说它在解包时找到了一个可选值 nil。 关于为什么会发生这种情况的任何想法?谢谢

【问题讨论】:

  • 你应该使用let layoutGuide = UILayoutGuide()
  • @byaruhaf 我使用了你的方法并且它运行了,但现在我收到了这个错误Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x600000538880 "new_app_3.SeeNewPostsButton:0x7fe15853d9f0.top"> and <NSLayoutYAxisAnchor:0x600000538f00 "UILayoutGuide:0x6000029e0a80''.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.
  • 注意部分称为创建布局指南:developer.apple.com/documentation/uikit/uilayoutguide
  • @PhillipMills 谢谢你的链接,这是很好的信息。

标签: swift xcode fatal-error


【解决方案1】:

使用:let layoutGuide = self.view.safeAreaLayoutGuide 而不是 var layoutGuide:UILayoutGuide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多