我是一名高中生,为学校项目创建一个简单的应用程序。这是我第一次使用Swift语言和使用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-04-13 12:13:04.411按钮[95215:59212987] - [ NSCFNumber _getCString:maxLength:encoding:]:无法识别的选择器发送到实例0xb000000000000003 2017-04-13 12:13:04.574按钮[95215:59212987] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFNumber _getCString:maxLength:encoding:]:无法识别的选择器发送到实例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 0x000000010b43e77d CFLocaleCreateCanonicalLocaleIdentifierFromString + 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 15 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) }
知道问题是什么吗?
答案 0 :(得分:1)
修好后,清理构建(cmd + shift + k)并重新运行。
如果它仍然崩溃, 在你的所有课程中放置断点并找出崩溃的确切位置。请给我们一些额外的信息,我们会尝试帮助更多
注意:不需要同步
答案 1 :(得分:0)
在ViewDidLoad方法中提供断点。如果它在到达之前就崩溃了。然后只需检查storyBoard中的连接。您可能删除了一些连接,故事板可能仍在尝试找到它。