Here is my code (largeAsteroids.count is never 0):
var largeAsteroids=[[SKTexture]]()
func randomLargeAsteroidTextures()->Array<SKTexture>{
let i=Int(arc4random())%largeAsteroids.count
return largeAsteroids[i]// this line triggers EXC_BREAKPOINT
}
When I execute my code, I receive no errors but I get a EXC_BREAKPOINT. I ensured there wasn't any breakpoint and at index i there was a valid object.
First I changed SKTexture to AnyObject, it didn't help. Then I tried to use NSMutableArray instead of swift array, problem still exist:
var largeAsteroids=NSMutableArray()
func randomLargeAsteroidTextures()->AnyObject{
let i=Int(arc4random())%largeAsteroids.count
return largeAsteroids.objectAtIndex(i) // this line triggers EXC_BREAKPOINT
}
update:
Problem solved, replace:
let i=Int(arc4random())%largeAsteroids.count
by:
let i=Int(arc4random_uniform(UInt32(largeAsteroids.count)))
Thanks for Matt's solution:
You should probably be using arc4random_uniform. You'll get modulo bias from your current implementation. – Matt Gibson