I'm trying to make a path (CGMutablePath
) to be used for a shape node in SpriteKit (SKShapeNode
). I'm running my code and it stops and says EXC_BREAKPOINT
as if there's a breakpoint in the compiler, when there isn't one. Below is a screenshot of what I have.
As you can see, I have no breakpoint set in the compiler. It just tends to stop at line 31. There's nothing in the logs that give a reason for this crash, if there were one to begin with.
I'm used to Objective-C stopping in main.m
with Thread 1: SIGABRT
and an NSException
in the log, but this just makes no sense. Can someone please explain what's wrong?
EDIT: here are more screenshots to prove I have no breakpoints set. When I get an exception, it simply throws a breakpoint.
Exception breakpoints are trivial to set up: go to the Breakpoint Navigation (Cmd+7), then click the + button in the bottom left and choose Add Exception Breakpoint.
How to add an Exception Breakpoint? Step 1 − To add an exception breakpoint, first move to breakpoint navigator in xcode. Step 2 − Click on the + option at the left bottom of the navigator and select Exception breakpoint. Once you select exception breakpoint, it will be added to our code.
That exception happens because arc4random()
returns UInt32
and you are subtracting a value of that, which possibly causes a negative value, which is not representable as an unsigned integer.
To fix this you may want to cast the expression to Int32 before subtracting:
var y = CGFloat(self.size.height / 3) + CGFloat((Int32)(arc4random() % 100) - 50)
Regarding the x
value above - you create CGFloat
and only subtract after that and therefore not encounter the above situation. Therefore a different fix would be the following
var y = CGFloat(self.size.height / 3) + CGFloat(arc4random() % 100) - 50
You basically have to ensure that at the point where you subtract something you have no unsigned type present.
The annoying thing about this is that the compiler does not warn you and the code actually works sometimes, every time when arc4random
returns something larger than 50 and will therefore not drift into the negative values...
Incorporating the feedback and suggestion that you should not use arc4random % something
the best solution would be to use arc4random_uniform
.
arc4random % something
will not yield a proper distribution of the random values, while arc4random_uniform(something)
does - this has already been discussed on SO before.
A final note: you probably should choose 101 as upper bound instead of 100 because both arc4random
as well as arc4random_uniform
produce a value between 0 and (upper-1), e.g. 0 and 99. If you subtract 50 you get a value between -50 and +49. Choosing 101 will yield the desired range of -50 to +50.
var y = CGFloat(self.size.height / 3) + CGFloat(arc4random_uniform(101)) - 50
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With