Swift - 数组索引超出范围警告

时间:2020-12-08 07:40:42

My code has an issue when I run the iOS simulator. It breaks and brings me to the line of code:let targetUser = users[indexPath.row] and says 'EXC_BAD_INSTRUCTION (CODE=EXC_1386_INVOP,snbcode = 0x0)' would anyone be able to help me figure out why?

我运行iOS模拟器时,我的代码有问题。它打破并带我到代码行:让targetUser = users [indexPath.row]并说'EXC_BAD_INSTRUCTION(CODE = EXC_1386_INVOP,snbcode = 0x0)'任何人都可以帮我找出原因吗?

class OverviewTableViewController: UITableViewController {


@IBOutlet weak var LogoutButton: UIBarButtonItem!
@IBOutlet weak var ChoosePartnerButton: UIBarButtonItem!

var rooms = [PFObject]()
var users = [PFUser]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.setLeftBarButtonItem(LogoutButton, animated: false)
    self.navigationItem.setRightBarButtonItem(ChoosePartnerButton, animated: false)
}


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if PFUser.currentUser() != nil {
        loadData()
    }


}

func loadData() {
    rooms = [PFObject]()
    users = [PFUser]()

    self.tableView.reloadData()

    let pred = NSPredicate(format: "user1 = %@ OR user2 = %@", PFUser.currentUser()!, PFUser.currentUser()!)

    let roomQuery = PFQuery(className: "Room", predicate: pred)
    roomQuery.includeKey("user1")
    roomQuery.includeKey("user2")

    roomQuery.findObjectsInBackgroundWithBlock { (results:[AnyObject]?, error:NSError?) -> Void in
        if error == nil {
            self.rooms = results as! [PFObject]

            for room in self.rooms {
                let user1 = room.objectForKey("user1") as! PFUser
                let user2 = room["user2"] as! PFUser

                if user1.objectId != PFUser.currentUser()?.objectId {
                    self.users.append(user1)
                }

                if user2.objectId != PFUser.currentUser()?.objectId {
                    self.users.append(user2)
                }

            }

            self.tableView.reloadData()


        }
    }


}


    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return rooms.count
    }

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 80
}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! OverviewTableViewCell

    let targetUser = users[indexPath.row]

    cell.nameLabel.text = targetUser.username



return cell
}

1 个解决方案

#1


Sounds like @MartinR had nailed it. You're reading rooms.count in numberOfRowsInSection, but then looking up data from the users array in cellForRowAtIndexPath.

听起来像@MartinR已经钉了它。您正在读取numberOfRowsInSection中的rooms.count,但随后在cellForRowAtIndexPath中查找users数组中的数据。

You could figure this out in the debugger by examining indexPath.row when you crash, and examining the size of rooms.count as well.

您可以通过在崩溃时检查indexPath.row并检查rooms.count的大小来在调试器中解决这个问题。

#1


Sounds like @MartinR had nailed it. You're reading rooms.count in numberOfRowsInSection, but then looking up data from the users array in cellForRowAtIndexPath.

听起来像@MartinR已经钉了它。您正在读取numberOfRowsInSection中的rooms.count,但随后在cellForRowAtIndexPath中查找users数组中的数据。

You could figure this out in the debugger by examining indexPath.row when you crash, and examining the size of rooms.count as well.

您可以通过在崩溃时检查indexPath.row并检查rooms.count的大小来在调试器中解决这个问题。