ios - 解包可选值时意外发现 nil

标签 ios swift fatal-error dispatch-async optional-values

我正在尝试在点击 postVC 上的按钮加载数据并导航到 guestVC 后从 Parse 服务器加载数据。它工作正常,但在某个时候开始使应用程序崩溃......不知道为什么?我收到 fatal error :在展开可选值时意外发现 nil...任何和所有方向或帮助将不胜感激。谢谢!

import UIKit
import Parse

var postuuid = [String]()

class postVC: UITableViewController {

//postVC button click function
//clicked username button from post
    @IBAction func usernameBtn_click(sender: AnyObject) {
        let i = sender.layer.valueForKey("index") as! NSIndexPath
        let cell = tableView.cellForRowAtIndexPath(i) as! postCell

        // if user tapped on himself go home, else go guest
        if cell.usernameBtn.titleLabel?.text == PFUser.currentUser()?.username {
            let home = self.storyboard?.instantiateViewControllerWithIdentifier("homeVC") as! homeVC
            self.navigationController?.pushViewController(home, animated: true)    
        } else {
            let guest = self.storyboard?.instantiateViewControllerWithIdentifier("guestVC") as! guestVC
            self.navigationController?.pushViewController(guest, animated: true)
        }  
    } 



// guestVC relevant code
import UIKit
import Parse

var guestname = [String]()

class guestVC: UICollectionViewController {

    var uuidArray = [String]()
    var picArray = [PFFile]()

    // posts loading function
    func loadPosts() {     

            let query = PFQuery(className: "posts")
// app keeps crashing in line below when I try to load data to guestVC
            query.whereKey("username", equalTo: guestname.last!)
            query.limit = self.page
            query.findObjectsInBackgroundWithBlock( { (objects:[PFObject]?, error:NSError?) -> Void in

                if error == nil {

                    self.uuidArray.removeAll(keepCapacity: false)
                    self.picArray.removeAll(keepCapacity: false)

                    for object in objects! {
                        self.uuidArray.append(object.valueForKey("uuid") as! String)
                        self.picArray.append(object.valueForKey("pic") as! PFFile)
                    }

                    self.collectionView?.reloadData()
                } else {
                    print(error!.localizedDescription)
                }
                dispatch_async(dispatch_get_main_queue(), { () -> Void in

                    // code here will be executed as the main queue

                })
            })

    }

最佳答案

您使用大量感叹号来强制解开代码中的可选值,这是一个坏习惯。 例如,您可以通过以下方式安全地解开 guestname.last:

guard let lastItem = guestname.last else {
// do something else
return
}
query.whereKey("username", equalTo: lastItem)

关于ios - 解包可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051260/

相关文章:

ios - 返回时发生奇怪的崩溃

ios - 无法加载 bundle : NSBundle in iOS Swift? 中的 NIB

swift - Swift 中 void -> (void) 的含义是什么?

visual-studio - Visual Studio 在调试 .NET 4.0 应用程序时使用 .NET 4.5 调试 fatal error 0x8007000e

android - 我的应用程序在致命故障中在模拟器中崩溃

wordpress - HHVM 在 fatal error 时输出状态代码 200,因此我无法使用 fastcgi_next_upstream

ios - 无法编译 React Native iOS 应用程序以在物理设备上运行,React Native Firebase 出现问题

ios - 如何在 Xcode 上编辑自动生成的评论模板?

objective-c - 如何在 iOS 中获取照片的原始文件名?

ios - 使用swift访问解析中的类