从一个空数组检索值 [英] Retrieving values from an empty array

查看:131
本文介绍了从一个空数组检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户上传图像和其他用户离开答复的形象。一切工作正常,到目前为止,除非该特定图像对象行是空的,应用程序崩溃。

 致命错误:意外地发现零而展开的可选值

下面是我的code:

 覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()    VAR误差=    VAR的查询= PFQuery(的className:信息)
    query.getObjectInBackgroundWithId(CELLID){
        (!对象:PFObject!错误:NSError) - GT;在无效
        如果错误== {为零
            VAR阵列= objects.objectForKey(回复)作为[字符串] //< - 当错误发生在这里的编译器点。
            对于数组对象{
                self.repliesArray.append(对象为String)
            }
        }其他{
            self.displayError(错误,错误:错误retreiving)
        }
        self.tableView.reloadData()
    }
}


解决方案

  query.getObjectInBackgroundWithId(CELLID){
    (!对象:PFObject!错误:NSError) - GT;在无效
    如果错误== {为零
        VAR阵列= objects.objectForKey(回复)作为[字符串] //< - 当错误发生在这里的编译器点。

这里的关键是,你要接受一个隐含展开可选的( PFObject!),这是一个暗示承诺,它永远不会是,但

在理想情况下(*),类型应该是(对象:PFObject?错误:NSError?) - GT;虚空来使自选明确,强迫你做检查是必需的。我相信你能做到这一点,即使调用者声称送你(PFObject!NSError!) - GT;虚空,因为我认为斯威夫特会让implicit->显式转换为你。

如果这仍然是不可能的,那么你将不得不手动验证对象不是使用它之前。 (这是不同于@新德里的解决方案,它会检查的事情包含的在对象都是非 - 这不是问题;问题是,对象本身就是

(*)我说理想在这里现有的接口方面。传递可选值/错误的元组是一个糟糕的模式。更好的解决方案是使用像一个 结果对象

I want the user to upload an image, and other users leave replies to that image. Everything works fine so far except if the row for that specific image object is empty, the app crashes.

fatal error: unexpectedly found nil while unwrapping an Optional value

Here is my code :

override func viewDidLoad() {
    super.viewDidLoad()

    var error = ""

    var query = PFQuery(className:"Posts")
    query.getObjectInBackgroundWithId(cellID) {
        (objects: PFObject!, error: NSError!) -> Void in
        if error == nil {      
            var array = objects.objectForKey("replies") as [String] // <- when error occurs the compiler point here.
            for object in  array {
                self.repliesArray.append(object as String)
            }
        } else { 
            self.displayError("Error", error: "Error retreiving")
        }
        self.tableView.reloadData()
    }
}

解决方案

query.getObjectInBackgroundWithId(cellID) {
    (objects: PFObject!, error: NSError!) -> Void in
    if error == nil {      
        var array = objects.objectForKey("replies") as [String] // <- when error occurs the compiler point here.

The key here is that you're accepting an implicitly unwrapped optional (PFObject!) which is implying a promise that it will never be nil, but is nil.

Ideally (*), the type should be (objects: PFObject?, error: NSError?) -> Void to make the optionals explicit and force you to do the nil checking that is required. I believe you can do this even if the caller claims to send you (PFObject!, NSError!) -> Void, since I think Swift will make the implicit->explicit conversion for you.

If that's still impossible, then you will have to manually verify that objects is not nil before using it. (This is unlike @Dehli's solution, which checks that the things contained in objects are non-nil. That's not the problem; the problem is that objects itself is nil.)

(*) I say "ideally" here in terms of the existing interface. Passing a tuple of optional value/error is a lousy pattern. The better solution is to use something like a Result object.

这篇关于从一个空数组检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆