0

这是代码:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    @IBAction func changeBtn(_ sender: NSButton) {
        obj1.changeColor()
        obj2.changeBackgroundColor()
        objFld1.textColor = obj1.color
        objFld2.drawsBackground = true
        objFld2.backgroundColor = obj2.backgroundColor
    }

    @IBOutlet weak var objFld1: NSTextField!
    @IBOutlet weak var objFld2: NSTextField!
    class colored {
        var color = NSColor.black
        var backgroundColor = NSColor.gray
        func changeColor() {
            color = NSColor.yellow
        }
        func changeBackgroundColor() {
            backgroundColor = NSColor.blue
        }
    }

    var obj1: colored
    var obj2: colored
    override init() {
        obj1 = colored()
        obj2 = colored()
    }

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}

一切看起来都是无辜的,编译和启动都很好,但是当我点击“更改”按钮时,我得到了一个错误(在调试模式下):

在此处输入图像描述

在发布模式下,错误出现在不同的地方:

在此处输入图像描述

知道出了什么问题吗?

macOS Sierra,Xcode 8.1 (8B62)

调试会话给了我这些信息:

InheritProgram`@objc AppDelegate.changeBtn(NSButton) -> ():
    0x100001f10 <+0>:  pushq  %rbp
    0x100001f11 <+1>:  movq   %rsp, %rbp
    0x100001f14 <+4>:  subq   $0x30, %rsp
    0x100001f18 <+8>:  movq   %rdi, -0x8(%rbp)
    0x100001f1c <+12>: movq   %rdx, %rdi
    0x100001f1f <+15>: movq   %rsi, -0x10(%rbp)
    0x100001f23 <+19>: movq   %rdx, -0x18(%rbp)
    0x100001f27 <+23>: callq  0x100002bd8               ; symbol stub for: objc_retain
    0x100001f2c <+28>: movq   -0x8(%rbp), %rdi
    0x100001f30 <+32>: movq   %rax, -0x20(%rbp)
    0x100001f34 <+36>: callq  0x100002bd8               ; symbol stub for: objc_retain
    0x100001f39 <+41>: movq   -0x18(%rbp), %rdi
    0x100001f3d <+45>: movq   -0x8(%rbp), %rsi
    0x100001f41 <+49>: movq   %rax, -0x28(%rbp)
    0x100001f45 <+53>: callq  0x100001bd0               ; InheritProgram.AppDelegate.changeBtn (__ObjC.NSButton) -> () at AppDelegate.swift:16
    0x100001f4a <+58>: movq   -0x8(%rbp), %rdi
    0x100001f4e <+62>: callq  0x100002bd2               ; symbol stub for: objc_release
    0x100001f53 <+67>: addq   $0x30, %rsp
    0x100001f57 <+71>: popq   %rbp
    0x100001f58 <+72>: retq  
4

2 回答 2

2

仍然不知道那里发生了什么,但是将代码复制到一个新项目中工作得很好。

于 2016-12-08T08:33:52.900 回答
0

在非常相似的情况下, Product > Clean 为我解决了同样的问题。一个简单的类在文件的第一个属性上因 EXC_BAD_ACCESS 而崩溃。

于 2017-08-06T21:43:50.790 回答