swift - HTHorizo​​ntalSelectionList 在展开可选值时意外发现 nil

标签 swift

..我会告诉你原因:

我正在使用以下 Pod:HTHorizontalSelectionList

如果我这样声明:

class RightViewController: UIViewController, HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource {

    var selectionList: HTHorizontalSelectionList!
}

编译时出现以下错误:

ld: warning: directory not found for option '-FTest'
Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_HTHorizontalSelectionList", referenced from:
      __TMaCSo25HTHorizontalSelectionList in RightViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

啊!? 什么!?

如果我像这样实现它,它编译得很好!

override func viewDidLoad() {
    super.viewDidLoad()

    var selectionList: HTHorizontalSelectionList!
    selectionList?.frame = CGRectMake(0, 0, self.view.frame.size.width, 40)
    selectionList?.delegate = self
    selectionList?.dataSource = self
    self.view.addSubview(selectionList)
}

...当然,我在 addSubview 行上收到错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

当我经常遇到这样的事情时,我发现很难理解 Swift 的工作原理。

最佳答案

I'm finding it incredibly difficult to understand how Swift works when I get stuff like this happening quite often

这没有什么困难的。您一开始将可选变量设置为nil。它保持nil。最终你尝试解开 nil 然后你崩溃了,因为你不能这样做:

var selectionList: HTHorizontalSelectionList! // it is nil
selectionList?.frame = CGRectMake(0, 0, self.view.frame.size.width, 40) // still nil, nothing happens
selectionList?.delegate = self // still nil, nothing happens
selectionList?.dataSource = self // still nil, nothing happens
self.view.addSubview(selectionList) // unwrap, crash

如果您不想崩溃,请为 selectionList 分配 nil 以外的实际值,例如实际的 HTHorizo​​ntalSelectionList。

关于swift - HTHorizo​​ntalSelectionList 在展开可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163531/

相关文章:

Swift 语言 : how to continue after a guard statement?

ios - 删除 SKSpriteNode 的所有副本

objective-c - [任何对象]?没有名为 'mutableCopy' 的成员

ios - 如何查询特定项目的解析并防止保存重复项

objective-c - 艺术家如何创建非线性抽象插值渐变图像

ios - Swift 和 Objc 的互操作性——可用性不仅仅在 objc 中起作用

ios - 如何在 UITextView 中为文本添加轮廓?

ios - 在用 Swift 编写的 Xcode 中的 iOS 应用程序中的 UITextView 中,将 UIButton 放置在文本的末尾

ios - 根据内容获取表格单元格的高度

IOS, swift : How come my subclass of UIScrollView doesn't scroll?