简体   繁体   中英

Swift Thread1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

There is a SS of error

This happened after add refresh method in to viewDidLoad for pull to refresh . Im using parse.com and that followers table is empty.

I found some questions about some error. But i didn't found anything same as my question or maybe i don't understand.

How can i fix this what is the problem ?

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)!

or the query-var is nil. That causes the exception.

You should probably use the if let construct

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

or if the code should be just executed when all the values exist you can use the guard keyword.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM