Annoying "expression result unused" warning - objective-c

This is for a stupidly simple animation but I keep getting this annoying "expression result unused" warning. Code is as follows:
-(IBAction)thingOneTapped:(UIGestureRecognizer *)sender{
if ((isLevel = YES)) {
thingOneEnded = YES;
goingToThingOne = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(otherThingToOne) userInfo:nil repeats:YES];
x = 11;
y = 118;
}
else (isLevel = NO); {
[self viewDidLoad];
}
}
-(void)otherThingToOne{
if ((isLevel = YES) && (isLevelOne = YES)) {
if ((x > 0) && (y > 0)) {
otherThing.center = CGPointMake(otherThing.center.x - .5, otherThing.center.y - 5.35);
x = x - .5;
y = y - 5.35;
}
else ((x < 0) && (y < 0)); {
[self levelOneDefault];
// EVIL WARNING IS RIGHT HERE IN THE ELSE PART
}
}

For checking equality you need to use == (i.e. Two equals signs) that's why you're getting the error. Also, your code will not work as you expect at the moment.
As mentioned by others.
Instead of using if(someBool == YES) just use if(someBool). And instead of if(someBool == NO) just use if(!someBool).
The reason it will not work at the moment is because (I think but not 100% on this) if(someBool = NO) will first set someBool to NO and then evaluate the condition which will always be if(NO) which will never enter the of block.
I think. But not sure about this as I haven't tested it.

You can also use like that without checking == operator, also it will be more faster:-
if ((isLevel) && (isLevelOne))

Related

For loop in Sprite Kit seems to be emptying an array?

I'm just starting to wrap my brain around Sprite Kit and I am encountering a very strange error when attempting to change the property of a node in a for loop im using.
I have two SKSpriteNode objects, one is the child of a SKScene (BLATheBugs) and the other is a child of the first (BLAEmptySpaces). I have a grid laid out with BLAEmptySpaces, and BLATheBugs on top of those empty spaces which are supposed to take UITouch, and move to an empty space if its bool isOccpupied property == False. When the scene is set up, the SKScene triggers a method in TheBugs:
-(void) spawnEmptySpacesInitialize
{
[self addChild:[self spawnEmptySpaces]];
}
which in turn triggers:
-(BLAEmptySpaces *) spawnEmptySpaces
{
emptySpace = [[BLAEmptySpaces alloc] init];
emptySpace.numberOfEmptySpacesNeeded = 12;
[emptySpace spawnEmptySpaces];
[emptySpace positionTheEmptySpaces];
return emptySpace;
}
which finally triggers a method in the EmptySpaces object:
-(BLAEmptySpaces *) spawnEmptySpaces
{
_emptySpacesArray = [NSMutableArray new];
for (int x = 0; x < _numberOfEmptySpacesNeeded; x++)
{
_anEmptySpace = [[BLAEmptySpaces alloc] initWithImageNamed:#"BlueLight.png"];
_anEmptySpace.zPosition = 50;
[_emptySpacesArray addObject:_anEmptySpace];
[self addChild: _anEmptySpace];
}
return self;
}
everything seems fine, (except for needing the additional "addChild" in the EmptySpaces object to get them to be drawn on the screen which i have also been trying to fix) but when i call the method to move TheBugs:
-(void) moveLeftOneSpace
{
NSLog(#"%d", emptySpace.emptySpacesArray.count);
for (emptySpace in emptySpace.emptySpacesArray)
{
NSLog(#"cycle");
if (emptySpace.isOccupied == NO)
{
for (_yellowBug in yellowBugArray)
{
if (_positionOfFingerTouchX > _yellowBug.position.x - variableOne && _positionOfFingerTouchX < _yellowBug.position.x + variableTwo && _positionOfFingerTouchY > _yellowBug.position.y - variableOne && _positionOfFingerTouchY < _yellowBug.position.y + variableTwo && emptySpace.position.x == _yellowBug.position.x - 80 && emptySpace.position.y == _yellowBug.position.y)
{
_yellowBug.position = CGPointMake(_yellowBug.position.x - spaceBetweenBugs, _yellowBug.position.y);
emptySpace.isOccupied = YES;
NSLog(#"execute");
}
}
}
}
}
It at first tells me there are 12 objects in the array and runs the operation. if I try to move any piece again, it tells me there are now NO objects in the array (yellowBugArray). It is also probably worth noting that it will not let me access emptySpace.anEmptySpace. Throws me an error.
Sorry for the long post, but hopefully somewhere in here is the cause of my problem.
Thank you very much guys!

Simple Code works in Debug, not in Release

So this isn't making sense at all. I have an extension method to NSMutableArray to move an item from one index to another. It's a fairly simple method and it works flawlessly when I compile my app in Debug Configuration. However, if I compile the app in Release Configuration, it crashes if I move from an item down (from index > to index). The crash isn't a an index out of bounds error. My local variable are being messed up and I have no idea why. Here's the entire method:
- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{
if (fromIndex == toIndex) return;
if (fromIndex >= self.count) return;
if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
id movingObject = [self objectAtIndex:fromIndex];
if (fromIndex < toIndex){
for (int i = fromIndex; i <= toIndex; i++){
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : [self objectAtIndex:i + 1]];
}
} else {
//The problem occurs in this block (though the crash doesn't always occur here)
id cObject;
id prevObject;
for (int i = toIndex; i <= fromIndex; i++){
//usually on the last loop, my "prevObject" become 'messed up' after the following line:
cObject = [self objectAtIndex:i];
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : prevObject];
prevObject = cObject;
}
}
}
I can't step through the code since it's a release build, but I've NSLogged the variables at each step through the loop. Usually, on the last loop the prevObject var is assigned some random variable when cObject = [self objectAtIndex:i]; completes. Sometimes it's set to nil but often it's some other random variable in my code. If it's nil the code crashes when I try to replace the object in the array. Otherwise, it crashes later on when I try to access the array and receive back the wrong object.
Does anyone have any idea what's going on? I mean, the problem is occurring in 4 lines of code, which I've been over a hundred times.
Ok, just as the problem/crash made no sense, so also the fix doesn't make any sense at all. I had started to just randomly change up the code to see if anything would work. Moving the line: prevObject = cObject to the very beginning of the loop fixed the problem. Doing this didn't change the logic at all... Nada... shouldn't have made a difference. And yet, it did. Whoever said programming is logical? Here's the code that works:
- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{
if (fromIndex == toIndex) return;
if (fromIndex >= self.count) return;
if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
id movingObject = [self objectAtIndex:fromIndex];
if (fromIndex < toIndex){
for (int i = fromIndex; i <= toIndex; i++){
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : [self objectAtIndex:i + 1]];
}
} else {
id cObject = nil;
id prevObject;
for (int i = toIndex; i <= fromIndex; i++){
prevObject = cObject;
cObject = [self objectAtIndex:i];
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : prevObject];
}
}
}
So, anybody want to chime in as to what's going on?

stackoverflow when try to pass variable to bool function

i create a counting timer(on a label) and a variable that contain the label integer value(named count). also i create a function that check if my number is 7, divided by 7 or contain 7. when i try to pass my count value to the check function my app is stack. i try for a long time to find why the stack overflow is occur but i didn't success. how can i solve it?
here is my code:
-(IBAction)start:(id)sender
{
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES];
MainInt = 0;
numbersTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(countup) userInfo:nil repeats:YES];
}
-(void)countup
{
MainInt += 1;
numbersLabel.text = [NSString stringWithFormat:#"%i", MainInt];
count = numbersLabel.text.intValue;
NSLog(#"num: %d", count);
if ([self checknum:(count) == false]) {
NSLog(#"BOOM");
}
}
-(BOOL)checknum:(int)number
{
while (number<10)
{
if(number ==7)
{
NSLog(#"boom, i=%d", number);
return true;
}
}
while (number>=10 && number<1000)
{
if(number % 7 == 0)
{
NSLog(#"boom i=%d", number);
return true;
}
if([self revese:(number)])
{
NSLog(#"boom reverse num = %d", number);
return true;
}
}
return false;
}
-(BOOL) revese:(int)number
{
if(number < 10 && number != 7)
return false;
if(((number % 10) == 7) || ((number / 10) == 7))
return true;
else {
[self revese:(number / 10)];
}
return false;
}
thanks!
The statement
if ([self checknum:(count) == false]) {
NSLog(#"BOOM");
}
is nonsense. You are effective asking
BOOL isLessThanOne = (count < 1);
if ([self checksum:isLessThanOne]) {
NSLog(#"BOOM");
}
change this for
if ([self checksum:count] == NO) {
NSLog(#"BOOM");
}
There are lots of issues but the first ones are these:
while (number<10)
//
while (number>=10 && number<1000)
You want an if/else type conditional statement here. The way you have it now, since you never adjust the value of number, you will get caught in an infinite loop if the strict conditions you are testing later in the code are not met. Something more like:
if(number<10){
// do some tests
} else if (number<1000){
// do some other tests
}
There are other issues but those are a start.
It is very hard to tell as I cannot really see what you are doing, but if you have stack overflow, it is very likely that your problem is the function revese which is recursive. I would bet that you have a certain value being passed to revese that is causing it to be called over and over again based on some of the unusual logic you are using in your conditional statements. You should really step through this function carefully with your debugger to identify why this is happening.

New to coding and would like some pointers regarding my Objective C code

I'm working my way through Stephan Kochan's book on Objective C and I've come up against a problem which I'm hoping someone can help with.
Here's my print method from a Fraction class that I've been working on:
-(void) print: (BOOL) reduce {
Fraction *resultReduced = [[Fraction alloc] init];
[resultReduced setTo:numerator over:denominator];
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
if (reduce) {
[resultReduced reduce];
if (resultReduced.denominator < 0) {
resultReduced.denominator = -resultReduced.denominator;
resultReduced.numerator = -resultReduced.numerator;
}
else if (resultReduced.denominator == 1)
NSLog (#"%i", resultReduced.numerator);
else if (resultReduced.numerator == 0)
NSLog (#"0");
else if (resultReduced.numerator == resultReduced.denominator)
NSLog (#"1");
else if (resultReduced.numerator > resultReduced.denominator || resultReduced.numerator < 0) {
[resultReduced improper];
if (resultReduced.whole == 0)
NSLog (#"%i/%i", resultReduced.numerator, resultReduced.denominator);
else if (resultReduced.whole < 0 && resultReduced.numerator < 0) {
resultReduced.numerator = -resultReduced.numerator;
NSLog (#"%i %i/%i", resultReduced.whole, resultReduced.numerator, resultReduced.denominator);
}
else
NSLog (#"%i %i/%i", resultReduced.whole, resultReduced.numerator, resultReduced.denominator);
}
else
NSLog (#"%i/%i", resultReduced.numerator, resultReduced.denominator);
}
else
NSLog (#"%i/%i", numerator, denominator);
}
The problem is that if I subtract 15/4 from 2/3 I get -37/12 which I'd like to display as -3 1/12 using this message [resultReduced improper] but it doesn't appear to work. If I change
else if (resultReduced.numerator > resultReduced.denominator || resultReduced.numerator < 0) {
to just if then it does.
Could someone please explain to me why using if works, but if else doesn't?
The "if" statement by itself will execute every time. The "else" will only execute if the "if" fails.

If statement expecting ]

if (menuPlayed == TRUE && [sender tag == 0]) {
NSLog(#"You're pressing the right button at the right time");
}
Any idea why this is throwing up a "Expected "]
"" error? I have absolutely no idea what's wrong with those comparisons :(
Change:
if (menuPlayed == TRUE && [sender tag == 0])
to:
if (menuPlayed == TRUE && [sender tag] == 0)
Also note that you should never write expressions such as menuPlayed == TRUE - always write this as just menuPlayed, i.e. in this particular case:
if (menuPlayed && [sender tag] == 0)
And as mentioned in #rokjarc's answer, you might want to add some parentheses for clarity, although these are not actually required:
if (menuPlayed && ([sender tag] == 0))
Change your code to:
if ((menuPlayed == TRUE) && ([sender tag] == 0))
And you'll probably have to typecast sender, something like
if ((menuPlayed == TRUE) && ([(UIButton *)sender tag] == 0))
Ofcourse you shouldn't just use (UIButton *) for typecast.
Use the correct class of your sender object or use one
of it's ancestor classes. I believe 'tag' is added to
UIView in this object hierarchy.