【问题标题】:App crashing even though nothing besides code in Xcode with Swift 2应用程序崩溃,即使除了 Xcode 中的 Swift 2 代码之外什么都没有
【发布时间】:2016-02-07 02:14:12
【问题描述】:

我尝试了很多不同的东西,也研究了很多,但我似乎无法解决这个问题。

下面的代码编译良好并且构建成功,但几秒钟后Xcode打开备份并使用经典线程1打开AppDelegate文件信号 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)
    }
}

【问题讨论】:

标签: ios swift debugging uicolor sigabrt


【解决方案1】:

函数名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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多