我已经尝试了很多不同的东西并对此进行了很多研究,但我似乎无法解决这个问题。
下面的代码可以很好地编译并构建成功,但几秒钟后 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/