【问题标题】:Swift Thread1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)Swift Thread1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
【发布时间】:2016-04-22 21:20:28
【问题描述】:

There is a SS of error

这发生在将 refresh 方法添加到 viewDidLoad 以进行 pull to refresh 之后。我正在使用 parse.com 并且关注者表是空的。

我发现了一些关于某些错误的问题。但我没有发现任何与我的问题相同的内容,或者我不明白。

我该如何解决这是什么问题?

import UIKit
import Parse

class TableViewController: UITableViewController {

    var usernames = [""]
    var userids = [""]
    var isFollowing = ["":false]

    var refresher: UIRefreshControl!

    func refresh() {

        var query = PFUser.query()

        query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if let users = objects {

                self.usernames.removeAll(keepCapacity: true)
                self.userids.removeAll(keepCapacity: true)
                self.isFollowing.removeAll(keepCapacity: true)

                for object in users {

                    if let user = object as? PFUser {

                        if user.objectId! != PFUser.currentUser()?.objectId {

                            self.usernames.append(user.username!)
                            self.userids.append(user.objectId!)

                            var query = PFQuery(className: "followers")

                            query.whereKey("follower", equalTo: (PFUser.currentUser()?.objectId)!)
                            query.whereKey("following", equalTo: user.objectId!)

                            query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                                if let objects = objects {

                                    if objects.count > 0 {

                                        self.isFollowing[user.objectId!] = true

                                    } else {

                                        self.isFollowing[user.objectId!] = false

                                    }
                                }

                                if self.isFollowing.count == self.usernames.count {

                                    self.tableView.reloadData()

                                    self.refresher.endRefreshing()

                                }


                            })



                        }
                    }

                }



            }



        })





    }



    override func viewDidLoad() {
        super.viewDidLoad()

        refresher = UIRefreshControl()
        refresher.attributedTitle = NSAttributedString(string: "Pull to refresher")
        refresher.addTarget(self , action: "refresh" , forControlEvents:  UIControlEvents.ValueChanged)
        self.tableView.addSubview(refresher)
        refresh()
            }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return usernames.count
    }



    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = usernames[indexPath.row]
        let followedOnjectId = userids[indexPath.row]
        if isFollowing[followedOnjectId] == true {
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        }
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
        let followedOnjectId = userids[indexPath.row]

        if isFollowing[followedOnjectId] == false{
            isFollowing[followedOnjectId] = true

        cell.accessoryType = UITableViewCellAccessoryType.Checkmark

        var following = PFObject(className: "followers")
        following["following"] = userids[indexPath.row]
        following["follower"] = PFUser.currentUser()?.objectId

        following.saveInBackground()
        }else
        {
            isFollowing[followedOnjectId] = false
            cell.accessoryType = UITableViewCellAccessoryType.None
            var query = PFQuery(className: "followers")
            query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
            query.whereKey("following", equalTo: userids[indexPath.row])
            query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
                if let objects = objects {
                    for objects in objects {
                    objects.deleteInBackground()
                    }
                }

            })


        }
    }

}

【问题讨论】:

  • 尝试打印 PFUser.currentUser?.objectId 看看是否为 nil。如果是,则表示用户未登录或未注册。

标签: ios xcode swift swift2


【解决方案1】:
(PFUser.currentUser()?.objectId)!

或者查询变量为零。这会导致异常。

你应该使用 if let 构造

if let objectId = PFUser.currentUser()?.objectId {
    query.whereKey("follower", equalTo: objectId)
}

或者如果代码应该在所有值都存在时才执行,您可以使用 guard 关键字。

guard let a = optionalType?.variable?.a, b = optionalType?.variable?.b else { return }

【讨论】:

  • 非常感谢你让我开心:)
  • 请分享控制台输出(右下窗口)
  • 其中一个值为 nil。 following["following"] = userids[indexPath.row] following["follower"] = PFUser.currentUser()?.objectId 是不允许的。你必须添加一个零检查。
  • 就像上面一样。如果让 varName = something.optionalVar? { // 在这里设置你的 val 有一个非零值 }
猜你喜欢
  • 1970-01-01
  • 2012-05-30
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2016-08-08
  • 2020-06-12
相关资源
最近更新 更多