gpt4 book ai didi

swift - 值不等于 nil 会使程序崩溃,错误为 "unexpectedly found nil while unwrapping an Optional value"

转载 作者:行者123 更新时间:2023-11-30 12:55:25 25 4
gpt4 key购买 nike

我正在尝试使用以下代码设置 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 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

最佳答案

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

关于swift - 值不等于 nil 会使程序崩溃,错误为 "unexpectedly found nil while unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40457357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com