我試圖用這個代碼來設置一個的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 = {}
}
,所以我們應該知道,該層是不爲零,可是當我恢復程序
我也用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 0xpartial 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
右鍵單擊視圖並確保只有一個連接 –
我檢查了視圖和viewController,是隻有一個連接。 – C1FR1
堆棧跟蹤顯示崩潰的內容是什麼? – rmaddy