.xib 文件(界面),显示身份检查器选项卡我是 Xcode 和 Swift 的新手,并试图运行一个简单的乘法游戏应用程序。但是,当我尝试在模拟器上运行测试时,收到 SIGABRT 信号错误,并且应用程序无法加载。我不知道如何解决这个问题,请帮忙!我已经尝试了一切,但似乎没有任何效果。注意:我没有使用故事板,并且已经从项目文件夹中删除了 main.storyboard 文件,重点是 xib 文件(我使用的是 Xcode 10.3)
我收到的错误是:
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类与键 inputField 的键值编码不兼容。”
// This is my AppDelegate. I receive the SIGABRT signal on the third line (class AppDelegate).
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mainVC: MainViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
mainVC = MainViewController(nibName: "MainViewController", bundle:nil)
let frame = UIScreen.main.bounds
window = UIWindow(frame:frame)
window!.rootViewController = mainVC
window!.makeKeyAndVisible()
return true
}
}
单独的文件
//MainViewController.swift, created with MainViewController.xib
import UIKit
class MainViewController: UIViewController
{
@IBOutlet weak var number1:UILabel?
@IBOutlet weak var number2:UILabel?
@IBOutlet weak var timeLabel:UILabel?
@IBOutlet weak var scorecounter:UILabel?
@IBOutlet weak var inputField:UITextField?
var score:Int = 0
var timer:Timer?
var seconds:Int = 60
override func viewDidLoad()
{
super.viewDidLoad()
setRandomNumberLabel1()
setRandomNumberLabel2()
updateScoreLabel()
inputField?.addTarget(self, action: #selector(textFieldDidChange(textField:)), for:UIControl.Event.editingChanged)
}
func updateScoreLabel()
{
scorecounter?.text = "\(score)"
}
func updateTimeLabel()
{
if(timeLabel != nil)
{
let min:Int = (seconds / 60) % 60
let sec:Int = seconds % 60
let min_p:String = String(format: "%02d", min)
let sec_p:String = String(format: "%02d", sec)
timeLabel!.text = "\(min_p):\(sec_p)"
}
}
func setRandomNumberLabel1()
{
number1?.text = generateRandomString()
}
func setRandomNumberLabel2()
{
number2?.text = generateRandomString()
}
@objc func textFieldDidChange(textField:UITextField)
{
if inputField?.text?.count ?? 0 < 1
{
return
}
if let number1_text = number1?.text,
let number2_text = number2?.text,
let input_text = inputField?.text,
let number1 = Int(number1_text),
let number2 = Int(number2_text),
let input = Int(input_text)
{
print("Comparing: \(input_text) == \(number1_text) times \(number2_text)")
if(input == number1 * number2)
{
print("Correct!")
score += 1
}
else
{
print("Incorrect!")
score -= 1
}
}
setRandomNumberLabel1()
setRandomNumberLabel2()
updateScoreLabel()
updateTimeLabel()
if(timer == nil)
{
timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector:#selector(onUpdateTimer), userInfo:nil, repeats:true)
}
}
@objc func onUpdateTimer() -> Void
{
if(seconds > 0 && seconds <= 60)
{
seconds -= 1
updateTimeLabel()
}
else if(seconds == 0)
{
if(timer != nil)
{
timer!.invalidate()
timer = nil
let alertController = UIAlertController(title: "Game Over!", message: "Time's up! You got a score of: \(score) points. Good Job!", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: nil)
alertController.addAction(restartAction)
self.present(alertController, animated: true, completion: nil)
score = 0
seconds = 60
updateTimeLabel()
updateScoreLabel()
setRandomNumberLabel1()
setRandomNumberLabel2()
}
}
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func generateRandomString() -> String
{
var result:String = ""
for _ in 1...1
{
let digit = Int.random(in: 1..<10)
result += "\(digit)"
}
return result
}
}