Swift中调用方法的EXC_BAD_ACCESS错误 [英] EXC_BAD_ACCESS error for invoking method in swift

查看:147
本文介绍了Swift中调用方法的EXC_BAD_ACCESS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

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
    }


}

所有内容看起来都是无辜的,可以编译并正常启动,但是当我单击更改"按钮时,出现了错误(在调试模式下):

Everything looks innocent and compiles and starts up just fine, but when I click the "change" button, I got an error (in debug mode):

在释放模式下,错误出现在不同的位置:

In release mode, the error appears at a different place:

关于出什么问题了吗?

macOS Sierra,Xcode 8.1(8B62)

macOS Sierra, Xcode 8.1 (8B62)

调试会话为我提供了以下信息:

The debug session gives me these info:

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  

推荐答案

仍然不知道在那里发生了什么,但是将代码复制到新项目中就可以了.

Still have no idea what happened there, but copying the code to a new project worked perfectly fine.

这篇关于Swift中调用方法的EXC_BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆