2017-04-11 105 views
-1

我是一名高中学生,为一个学校项目创建一个简单的应用程序。这是我第一次创建一个应用程序,使用Swift语言和使用Xcode。每次运行应用程序时,它在打开之前崩溃。这是它崩溃后显示的内容:Xcode中的崩溃应用程序

Error Message

这是代码:

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) }

的未捕获的异常终止

任何想法是什么问题?

+1

无关你的崩溃,但摆脱那些不必要的电话来'synchronize'。 – rmaddy

+0

我敢打赌,你的失败与IBOutlet/IBAction没有排列故事板有关。你可能在故事板中发起了一个无处不在的故事。 –

+1

请参阅http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1以了解如何调试崩溃。 – rmaddy

回答

0

在ViewDidLoad方法中给出一个断点。如果它在来之前崩溃。然后在storyBoard上检查你的连接。你可能已经删除了一些连接,故事板可能仍然试图找到。

+0

这应该是一个评论,因为它不能真正解决问题,它只是提供帮助调试问题的建议。 – rmaddy

1
  1. 检查XIB文件的文件所有者
  2. 检查你的视图控制器(出口和动作)的连接

定影那些后,清洁的构建(CMD +移+ k)和重新运行它。

如果它仍然崩溃, 会在所有类中放置断点并找出崩溃的位置。请给我们一些额外的信息,我们会尽力帮助更多

注:同步是没有必要的

+0

我是初学者,所以你必须为我拼出一些东西,对不起。我的应用程序中每个按钮都有.xib文件吗?如果是这样,他们的所有自定义类都是UIButton,我无法更改它们。 – Sidganz1234

+0

以下是堆栈跟踪: – Sidganz1234

+0

iOS屏幕可以通过以下方式开发: 1.以编程方式 –