2017-04-11 133 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.以編程方式 –