iphone NSInteger issues - iphone

This seems so basic but for some reason I can't get it to work - the 2 variables are both defined as NSIntegers:
if ([AScore == "100"] && [BScore == "100"]) {
...
}
That doesn't work - nor does it work when I take away the parentheses - nor does it work if I try to implement the 'isEqualToString' command. I'm sure this is a very basic mistake that i am making.

NSIntegers aren't objects, nor are strings integers.
Use if(AScore == 100 && BScore == 100) { instead.

Related

What is the most efficient to run certain lines of code only when a boolean is true?

If I had code that looked like the following:
if (bool1) {
statement1
statement2
} else if (bool2) {
statement3
statement4
}
and I only want to run statement 2 and 4 given another boolean (say bool3) is true, what is the best way to format that. I understand that I could add a nested if statement, but that seems bad from a maintainability perspective if I have 5 or more else ifs.
Any suggestions?
A nested if sounds perfectly fine to me at least. If none of the statements mess with bool1, bool3 and bool4 though and you really don't want to use nested ifs you could put them after eachother with conjunctions:
if (bool1) {
statement1
}
if (bool1 && bool3) {
statement2
}
if (bool2 && !bool1) {
statement3
}
if (bool2 && !bool1 && bool3) {
statement4
}
This looks terrible though so I'd just go with a plain and simple nested if.

Swift User Creation Not Detecting UIText Length

working on user creation. Pretty simple idea, the email can't be nil or empty, password and confirm password have to be equal to be each other, and password has to be over 6 characters. For whatever reason, this last condition is always passing true. Because of this, users are able to register with a 1 letter or number password. Why would this be happening?
else if(emailTxt.text != "" && passwordTxt.text == confirmPassTxt.text && passwordTxt.text?.characters.count > 6){}
Also, something interesting, I can obviously compare the values because this code to get an password mismatch works perfectly fine.
else if(emailTxt.text != "" && passwordTxt.text != confirmPassTxt.text)
This will return true or false correctly. So the problems come from counting the characters, but I just can't see it right now. Simple mistake?
Interesting way I solved this. Because I never gave an option for Swift to choose if the password WASN'T above 6 characters, it just assumed it was? Not sure why, but adding another condition like this worked.
lse if(emailTxt.text != "" && passwordTxt.text == confirmPassTxt.text && passwordTxt.text?.characters.count < 6 && confirmPassTxt.text?.characters.count < 6){
As you can see it is essentially the same except I changed the > symbols to <. Working perfectly fine now

formatWithString: Cocos2d-x passing in a CCString

Maybe i'm going about this wrong, and if i am please tell me because i just dont know any better.
But i am trying to pass in a CCString into the following format, and not having any luck. Could someone please tell me what the parameter for strings would be in C++ when passing them into another string?
Code:
CCString tilt = "";
if (recalculatedFrames >= 4 && (numberOfTimesRun > 0 && numberOfTimesRun < recalculatedFrames - 1)) {
tilt = "TR_";
}
initalTurnAnimationFrames->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::stringWithFormat("%s%d.png", tilt, i)));
Also is it fine to make a blank string like i am doing with tilt?
I think tilt should be class of string
And the CCString part should be
CCString::createWithFormat("%s%d.png", tilt.c_str(), i);
try this
CCString::stringWithFormat("%s%d.png", tilt.getCString(), i)

In Cocos2d is it possible to use multiple tags for a single object?

I want to be able to identify a group of objects for a CCSpriteBatchNode, but also identify a sub group of that group. To do something similar to this
CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
for (GameObject *tempObject in listOfGameObjects) {
if ([tempObject tag] == kBottleTagValue) {
//make bottle yellow
if ([tempObject tag] == kBrokenBottleTagValue) {
//also make bottle smaller
}
}
}
In the example all bottles would be turned yellow, and if the bottle was also tagged as broken it would be made smaller. So the broken bottle would need to be tagged with kBottleTagValue and kBrokenBottleTagValue. Is there away to do this? cause when I try to just add two tags it fails.
Yes you can do that using bit masking.
For example define your tag like that:
enum
{
kBottleTagValue = 1 << 0;
kBrokenBottleTagValue = 1 << 1;
};
Then tag your sprite:
[yoursprite setTag:kBottleTagValue|kBrokenBottleTagValue];
To finish you can check if your sprite is a broken bottles by doing something like that:
CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
for (GameObject *tempObject in listOfGameObjects)
{
if ([tempObject tag] & kBottleTagValue)
{
//make bottle yellow
if ([tempObject tag] & kBrokenBottleTagValue)
{
//also make bottle smaller
}
}
}
I hope it'll help you.
Using bitmasking is overkill and there's no need to abuse the tag property.
Speaking of properties: You can add a boolean property to your class and use if ([tempObject isClass:[BottleClass class]]) for the first logic gate.
I don't actually know Cocos2d but based on a quick reading, I guess you've descended GameObject, by whatever roundabout route, from CCNode? In that case the tag field is an integer. You can't store multiple values to it, but you could use it as a bit field. For example:
#define kTagValueBottle 0x0001
#define kTagValueBroken 0x0002
#define kTagValueAnotherAttribute 0x0004
#define kTagValueAThirdAttribute 0x0008
#define kTagValueAFourthAttribute 0x0010
/* etc */
You'd then assign the type as, for example:
object.tag = kTagValueBottle | kTagValueBroken;
So that computes the bitwise OR of kTagValueBottle and kTagValueBroken, and stores it as the new tag. You could also add a property at any time using a bitwise OR:
object.tag |= kTagValueBroken;
Or remove by using a bitwise AND with the inverse mask:
object.tag &= ~kTagValueBroken;
You'd replace your direct comparison tests with testing individual bits via a bitwise AND:
// if ([tempObject tag] == kBottleTagValue) // old test
if ([tempObject tag] & kBottleTagValue) // new test
This is the same sort of system that Apple use for properties like autoresizingFlags on UIView.
If you can handle reading example code in PHP rather than Objective-C, this seemed to be the most helpful article that I could find quickly through Google, though admittedly from slender pickings.

Boolean logic failure

I am having a strange problem with boolean logic. I must be doing something daft, but I can't figure it out.
In the below code firstMeasure.isInvisibleArea is true and measureBuffer1 is nil.
Even though test1 is evaluating to NO for some reason it is still dropping into my if statement.
It works ok if I use the commented out line.
Any idea why this happens?
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1 == nil;
BOOL test1 = measureBuffer1 == nil && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 == nil && !firstMeasure.isInVisibleArea)
//if (measureBufferNil && !firstVisible)
{
//do some action
}
Update 1:
I isolated the problem to !firstMeasure.isInVisibleArea as I've entirely taken on the measureBuffer bit.
Inside isInVisible area is a small calculation (it doesn't modify anything though), but the calculation is using self.view.frame. I am going take this out of the equation as well and see what happens. My hunch is that self.view.frame is changing between the two calls to isInVisibleArea.
Update 2:
This is indeed the problem. I have added the answer in more detail below
When in doubt, you should fully parenthesize. Without looking up the precedence rules, what I think what is happening is that = is getting higher precedence than == or &&. So try:
BOOL test1 = ((measureBuffer1 == nil) && !firstMeasure.isInVisibleArea);
While you certainly can parenthesize, you should also know that nil objects evaluate to boolean NO and non-nil objects evaluate to boolean YES. So you could just as easily write this:
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1;
BOOL test1 = !measureBuffer1 && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 && !firstMeasure.isInVisibleArea) {
//do some action
}
You would end up with the same results. I agree with GoatRider, though. It's always far better to parenthesize your conditional expressions to clarify what you really want to happen than it is to rely on the language's operator precedence to do it for you.
If test1 is evaluating to NO as you say, then drop test1 into the if statement:
if(test1){
//see if this executes?
}
See what that does.
My hunch was correct, it is related to the view frame changing between calls to firstMeasure.isInVisible area.
This whole routine is called in response to the view moving. I think I need to grab the value of firstMeasure.isInVisibleArea at the start of the method and use that value throughout.
Phew. Boolean logic isn't broken. All is right with the world.
Thanks for all your input