2016-11-07 49 views
0

我试图用这个代码来设置一个的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

+1

右键单击视图并确保只有一个连接 –

+0

我检查了视图和viewController,是只有一个连接。 – C1FR1

+0

堆栈跟踪显示崩溃的内容是什么? – rmaddy

回答

0

首先,确保你不也有一个名为DrawingView类,二,再也没有名称的属性出发用大写字母。

+0

我没有名为DrawingView的类,为什么我不应该使用大写字母来启动一个属性? – C1FR1

+1

因为违反公约和官方指南:https://swift.org/documentation/api-design-guidelines/ – PeejWeej

相关问题