简体   繁体   中英

iOS EXC_BAD_ACCESS, but nothing is nil

I'm getting a really strange EXC_BAD_ACCESS crash in my app. The code that causes the crash is

private func isValidGridCoordinate(coord : CGPoint) -> Bool {
    return !((coord.x < 0) || (coord.x >= gridSize.width)
        || (coord.y < 0) || (coord.y >= gridSize.height))
}

But when I look at the memory in XCode, I see that the values in coord, self and gridSize are exactly as expected. Why would this error happen if none of my objects are nil?

EDIT: The code that calls this function at the crash is

private func caveCellFromGridCoordinate(coord : CGPoint) -> CaveCell? {
    if (isValidGridCoordinate(coord)) {
        return self.grid[Int(coord.y)][Int(coord.x)] as? CaveCell
    }
    return nil
}

Looks like you are using | which is a bitwise OR operator. You want to use || -- the logical OR operator.

OK, so after putting in a bunch of println's, I found out that there was a logical error elsewhere in the code that was causing an overflow. Strange that I would get the error message I got, but anyway, it seems to work now

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