EXC_BREAKPOINT(代码= 1,子代码= 0x1051fbb50)控制台打印此:致命错误:索引超出范围

时间:2017-09-17 23:01:28

标签: swift

我是快速发展的新手,如果这个问题已经得到解答我很抱歉,但我不知道为什么我会收到这个错误。它将成功构建,但是当buttonTapped被点击时,我收到此错误并且应用程序崩溃。

这是我的代码:

import UIKit
import Firebase
import FBSDKLoginKit
import FirebaseAuth
import FBSDKCoreKit

class QuotesViewController: UIViewController {


    @IBOutlet weak var quoteLabel: UILabel!


    var quotes : [Quote] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        Database.database().reference().child("quotes").observe(DataEventType.childAdded, with: {(snapshot) in
            print(snapshot)

            let quote = Quote()
            quote.quote = (snapshot.value! as! NSDictionary)["quote"] as! String

            self.quotes.append(quote)
        })

    }

    @IBAction func buttonTapped(_ sender: Any) {
        newQuote()
    }


    @IBAction func didTappedLogout(_ sender: Any) {
        // sign user out of firebase
        let firebaseAuth = Auth.auth()
        do {
            try firebaseAuth.signOut()
        } catch let signOutError as NSError {
            print ("Error signing out: %@", signOutError)
        }

        // sign user out of Facebook
        FBSDKAccessToken.setCurrent(nil)
        let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
        let SignInViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "LoginView")

        self.present(SignInViewController, animated: true, completion: nil)
    }


    func newQuote(){  **this is where I get the error**
        let myQuote = quotes[Int(arc4random_uniform(UInt32(quotes.count) ))]  
        quoteLabel.text =  myQuote.quote
       // print(myQuote.quoteID)

    }
    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        newQuote()

    }
}

2 个答案:

答案 0 :(得分:0)

我猜你的应用程序崩溃是因为你在Firebase获取数据之前尝试访问quotes数组中的某个项目。因此,当您尝试从中获取某些内容时,其中没有任何内容,因此每个索引(甚至0)都超出范围。你应该先检查一下quotes中是否存在某些内容,如下所示:

func newQuote(){
    guard !quotes.isEmpty else{
        // Tell the user there are no quotes yet
        return
    }
    let myQuote = quotes[Int(arc4random_uniform(UInt32(quotes.count) ))]  
    quoteLabel.text =  myQuote.quote
}

答案 1 :(得分:0)

问题是signInSegue是这样的凭证功能:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        print(error)
        return
    }
    print("Successfully logged in with Facebook...")
    let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
    Auth.auth().signIn(with: credential) { (user, error) in
        print("User logged in...")



    }
    self.performSegue(withIdentifier: "signInSegue", sender: nil)
}

它应该是这样的:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        print(error)
        return
    }
    print("Successfully logged in with Facebook...")
    let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
    Auth.auth().signIn(with: credential) { (user, error) in
        print("User logged in...")
        self.performSegue(withIdentifier: "signInSegue", sender: nil)


    }

}