..我会告诉你原因:
我正在使用以下 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
以外的实际值,例如实际的 HTHorizontalSelectionList。
关于swift - HTHorizontalSelectionList 在展开可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163531/