ios - 应用程序崩溃,即使除了带有 Swift 2 的 Xcode 中的代码之外什么都没有

标签 ios swift debugging uicolor sigabrt

我已经尝试了很多不同的东西并对此进行了很多研究,但我似乎无法解决这个问题。

下面的代码可以很好地编译并构建成功,但几秒钟后 Xcode 会重新打开 AppDelegate 文件并打开 < strong>经典线程 1 发出 SIGABRT 信号,应用程序无法正常工作。

顺便说一句,该应用不应执行任何复杂的操作,而只是更改backgroundColor 的颜色。

请告诉我如何改变它并使其变得更好。

import UIKit

class ViewController: UIViewController {
    func changeBackround() {
        self.view.backgroundColor = UIColor.randomColor()
    }
    override func viewDidLoad() {
        NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("changeBackground"), userInfo: nil, repeats: true)   
    }    
}

extension UIColor {
    static func randomColor() -> UIColor {
        let red = CGFloat(drand48())
        let green = CGFloat(drand48())
        let blue = CGFloat(drand48())
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
}

最佳答案

错误的函数名称 changeBackround ,在你输入的选择器中 Selector("changeBackground") ,错误是因为它没有找到选择器对应的函数。

试试这个。

import UIKit

class ViewController: UIViewController {


    func changeBackground() {
        self.view.backgroundColor = UIColor.randomColor()
    }

    override func viewDidLoad() {

        NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("changeBackground"), userInfo: nil, repeats: true)

    }

}

extension UIColor {
    static func randomColor() -> UIColor {
        let red = CGFloat(drand48())
        let green = CGFloat(drand48())
        let blue = CGFloat(drand48())
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
}

关于ios - 应用程序崩溃,即使除了带有 Swift 2 的 Xcode 中的代码之外什么都没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248958/

相关文章:

java - 如何在编译期间使用java调试标志查找参数

Javascript Youtube mp4 下载到 ios

iphone - 使用多线程时如何锁定方法?

ios - Swift 5:解码嵌套JSON

swift - 如何在 Swift 中抑制返回类型为 Void 的闭包的隐式返回

ubuntu - 调试时如何加载 gdbinit?

ios - 如何检测滑动手势方向?

ios - 如何使用 "\t"右对齐或左对齐?

swift - XMLRPC one2many,many2one 连接 SWIFT 4

debugging - Bootstrap 默认调试脚本代码 - 有必要吗?