值不等于nil崩溃程序"在展开可选值时意外发现nil"

时间:2016-11-07 03:25:06

标签: swift swift3 nsview

我正在尝试使用以下代码设置NSView的背景颜色:

@IBOutlet weak var DrawingView: NSView!
override func viewDidLoad() {
    super.viewDidLoad()
    DrawingView.wantsLayer = true
}
override func awakeFromNib() {
    super.awakeFromNib()
    DrawingView.layer?.backgroundColor = NSColor.white.cgColor//crashes here
}

@IBOutlet已连接,但只要我调用DrawingView.layer,程序就崩溃了:

"unexpectedly found nil when unwrapping an optional value" 

甚至在印刷声明中。如果我使用控制台打印DrawingView.layer,则会返回。

(CALayer?) $R2 = 0x0000608000049930 {
  ObjectiveC.NSObject = {}
}

所以我们应该知道图层不是nil,但是当我恢复程序时错误发生了

我也尝试使用ViewWillAppear()代替awakeFromNib(),但结果两种方式相同。

我发誓我已经做了一千次没有任何问题,但有什么可能导致这个奇怪的错误吗?

堆栈跟踪:

0    libswiftCore.dylib                 0x00000001002f3cc0 swift_reportError + 132
1    libswiftCore.dylib                 0x0000000100311070 _swift_stdlib_reportFatalError + 61
2    libswiftCore.dylib                 0x00000001001070a0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
3    libswiftCore.dylib                 0x0000000100283210 partial apply for (_fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never).(closure #2) + 109
4    libswiftCore.dylib                 0x00000001001070a0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
5    libswiftCore.dylib                 0x000000010023b3d0 specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never + 96
6    ReeperG levelmaker                 0x0000000100001e00 ViewController.awakeFromNib() -> () + 177
7    ReeperG levelmaker                 0x0000000100002050 @objc ViewController.awakeFromNib() -> () + 34
8    CoreFoundation                     0x00007fffcc7012e0 -[NSSet makeObjectsPerformSelector:] + 217
9    AppKit                             0x00007fffca353771 -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1442
10   AppKit                             0x00007fffca45bd46 -[NSNib _instantiateNibWithExternalNameTable:options:] + 696
11   AppKit                             0x00007fffca45bba3 -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 143
12   AppKit                             0x00007fffcab1069b -[NSStoryboard instantiateControllerWithIdentifier:] + 234
13   AppKit                             0x00007fffca812d46 -[NSStoryboardSegueTemplate _perform:] + 70
14   AppKit                             0x00007fffca3cbaf3 -[NSViewController _loadViewIfRequired] + 300
15   AppKit                             0x00007fffca3cba8b -[NSViewController view] + 30
16   AppKit                             0x00007fffca563760 -[NSWindow _contentViewControllerChanged] + 109
17   Foundation                         0x00007fffce129bb8 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 329
18   AppKit                             0x00007fffca5ab2a0 -[NSWindow setValue:forKey:] + 112
19   AppKit                             0x00007fffca5aafb6 -[NSIBUserDefinedRuntimeAttributesConnector establishConnection] + 694
20   AppKit                             0x00007fffca353771 -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1284
21   AppKit                             0x00007fffca45bd46 -[NSNib _instantiateNibWithExternalNameTable:options:] + 696
22   AppKit                             0x00007fffca45bba3 -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 143
23   AppKit                             0x00007fffcab1069b -[NSStoryboard instantiateControllerWithIdentifier:] + 234
24   AppKit                             0x00007fffca344cd8 NSApplicationMain + 780
25   ReeperG levelmaker                 0x0000000100003860 main + 84
26   libdyld.dylib                      0x00007fffe17c9254 start + 1

该计划的名称是ReeperG levelmaker

1 个答案:

答案 0 :(得分:0)

首先,确保你没有名为DrawingView的类,其次,再也不要命名以大写字母开头的属性。