我是一名高中学生,为一个学校项目创建一个简单的应用程序。这是我第一次创建一个应用程序,使用Swift语言和使用Xcode。每次运行应用程序时,它在打开之前崩溃。这是它崩溃后显示的内容:Xcode中的崩溃应用程序
这是代码:
import UIKit
import Foundation
class ViewController: UIViewController {
@IBOutlet weak var coffeeOutput: UILabel!
@IBOutlet weak var check1: UILabel!
let things = UserDefaults.standard
var coffeeShops = [0,0,0,0,0,0,0,0,0]
// 0 days: Int = 0
// 1 highland: Int = 0
// 2 starbucks: Int = 0
// 3 heine: Int = 0
// 4 quills: Int = 0
// 5 bean: Int = 0
// 6 sunergos: Int = 0
// 7 press: Int = 0
// 8 please: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func highlands(sender: UIButton) {
things.set(things.integer(forKey: "highland")+1, forKey: "highland")
things.set(things.integer(forKey: "days")+1, forKey: "days")
things.synchronize()
}
@IBAction func downtown(_:AnyObject) {
things.set(things.integer(forKey: "please")+1, forKey: "please")
things.set(things.integer(forKey: "starbucks")+1, forKey: "starbucks")
things.set(things.integer(forKey: "press")+1, forKey: "press")
things.synchronize()
}
@IBAction func germantown(_:AnyObject) {
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.set(things.integer(forKey: "bean")+1, forKey: "bean")
things.set(things.integer(forKey: "sunergos")+1, forKey: "sunergos")
things.synchronize()
}
@IBAction func school(_:AnyObject) {
things.set(things.integer(forKey: "quills")+1, forKey: "quills")
things.synchronize()
}
@IBAction func homework(_:AnyObject) {
things.set(things.integer(forKey: "days")+1, forKey: "days")
things.set(things.integer(forKey: "starbucks")+1, forKey: "starbucks")
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.set(things.integer(forKey: "quills")+1, forKey: "quills")
things.set(things.integer(forKey: "sunergos")+1, forKey: "sunergos")
things.synchronize()
}
@IBAction func meeting(_:AnyObject) {
things.set(things.integer(forKey: "days")+1, forKey: "days")
things.set(things.integer(forKey: "starbucks")+1, forKey: "starbucks")
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.set(things.integer(forKey: "quills")+1, forKey: "quills")
things.set(things.integer(forKey: "bean")+1, forKey: "bean")
things.set(things.integer(forKey: "press")+1, forKey: "press")
things.synchronize()
}
@IBAction func toGo(_:AnyObject) {
things.set(things.integer(forKey: "starbucks")+1, forKey: "starbucks")
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.set(things.integer(forKey: "bean")+1, forKey: "bean")
things.set(things.integer(forKey: "sunergos")+1, forKey: "sunergos")
things.set(things.integer(forKey: "please")+1, forKey: "please")
things.synchronize()
}
@IBAction func hang(_:AnyObject) {
things.set(things.integer(forKey: "highland")+1, forKey: "highland")
things.set(things.integer(forKey: "days")+1, forKey: "days")
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.synchronize()
}
@IBAction func morning(_:AnyObject) {
things.set(things.integer(forKey: "days")+1, forKey: "days")
things.set(things.integer(forKey: "starbucks")+1, forKey: "starbucks")
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.set(things.integer(forKey: "bean")+1, forKey: "bean")
things.set(things.integer(forKey: "sunergos")+1, forKey: "sunergos")
things.set(things.integer(forKey: "press")+1, forKey: "press")
things.synchronize()
}
@IBAction func midday(_:AnyObject) {
//0 2 3 4 6 7 8
things.set(things.integer(forKey: "days")+1, forKey: "days")
things.set(things.integer(forKey: "starbucks")+1, forKey: "starbucks")
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.set(things.integer(forKey: "quills")+1, forKey: "quills")
things.set(things.integer(forKey: "sunergos")+1, forKey: "sunergos")
things.set(things.integer(forKey: "press")+1, forKey: "press")
things.set(things.integer(forKey: "please")+1, forKey: "please")
things.synchronize()
}
@IBAction func night(_:AnyObject) {
things.set(things.integer(forKey: "days")+1, forKey: "days")
things.set(things.integer(forKey: "highland")+1, forKey: "highland")
things.set(things.integer(forKey: "heine")+1, forKey: "heine")
things.synchronize()
}
// 0 days: Int = 0
// 1 highland: Int = 0
// 2 starbucks: Int = 0
// 3 heine: Int = 0
// 4 quills: Int = 0
// 5 bean: Int = 0
// 6 sunergos: Int = 0
// 7 press: Int = 0
// 8 please: Int = 0
@IBAction func computeShop(_:AnyObject) {
var max = things.integer(forKey: "days")
var maxKey = "days"
for element in UserDefaults.standard.dictionaryRepresentation().keys.elements{
if(things.integer(forKey: element)>max){
max = things.integer(forKey: element)
maxKey = element
}
}
self.coffeeOutput.text = maxKey
for element in UserDefaults.standard.dictionaryRepresentation().keys.elements{
things.set(0,forKey: element)
things.synchronize()
}
}
}
这里是堆栈跟踪: {
2017年4月13日12点13: 04.411按钮[95215:59212987] - [NSCFNumber _getCString:maxLength:encoding:]:无法识别的选择器发送到实例0xb000000000000003 2017-04-13 12:13:04.574按钮[95215:59212987] ***终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因: ' - [__ NSCFNumber _getCString:最大长度:编码:]:无法识别的选择发送到实例0xb000000000000003' ***第一掷调用堆栈: ( 0的CoreFoundation 0x000000010b4f4d4b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010af5621e objc_exception_throw + 48 2的CoreFoundation 0x000000010b564f04 - [NSObject的(NSObject的)doesNotRecognizeSelector:] + 132 3的CoreFoundation 0x000000010b47a005 ___forwarding_ + 1013 4的CoreFoundation 0x000000010b479b88 _CF_forwarding_prep_0 + 120 5的CoreFoundation 0x000000010b42e89b CFStringGetCString + 171 6的CoreFoundation 0x000000010b43e77dCFLocaleCreateCanonicalLocaleIdentifierFromString + 77 7的CoreFoundation 0x000000010b43e327 _CFLocaleCopyCurrentGuts + 487 8的CoreFoundation 0x000000010b43e109 + [NSLocale currentLocale] + 9 9基金会0x000000010aa19376 - [NSUserDefaults的(NSUserDefaults的)INIT] + 1637 10基金会0x000000010aa18cb5 + [NSUserDefaults的(NSUserDefaults的)standardUserDefaults] + 81 11 UIKit的0x000000010b9438df ___UIApplicationMainPreparations_block_invoke_2 + 53 12 libdispatch.dylib 0x000000010eb7d978 _dispatch_call_block_and_release + 12 13 libdispatch.dylib 0x000000010eba70cd _dispatch_client_callout + 8 14 libdispatch.dylib 0x000000010eb8865f _dispatch_root_queue_drain + 1450 1 5 libdispatch.dylib 0x000000010eb88059 _dispatch_worker_thread3 + 123 16 libsystem_pthread.dylib 0x000000010ef56736 _pthread_wqthread + 1299 17 libsystem_pthread.dylib 0x000000010ef56211 start_wqthread + 13 ) 的libC++ abi.dylib:与类型NSException (LLDB) }
的未捕获的异常终止任何想法是什么问题?
无关你的崩溃,但摆脱那些不必要的电话来'synchronize'。 – rmaddy
我敢打赌,你的失败与IBOutlet/IBAction没有排列故事板有关。你可能在故事板中发起了一个无处不在的故事。 –
请参阅http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1以了解如何调试崩溃。 – rmaddy