Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 14    Views: 64

awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
08/16/13 08:58 AM (12 years ago)

quick and simple question about logical operators

hey guys! i just need to ask if this is possible: if (conditiona && conditionb || condition c) { } what im trying to do is have a working code for conditiona + condition b OR condtiona + condition c is that possible? or should i just make it as if ((conditiona && conditionb) || (conditiona && condition c)) { }
 
Michael Travis
Aspiring developer
Profile
Posts: 38
Reg: Jan 24, 2013
Baton Rouge, LA
3,430
like
08/16/13 09:01 AM (12 years ago)
The bottom solution would be the correct one.
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/16/13 09:08 AM (12 years ago)
thanks for the quick response! yes i did the second one and got no errors. although it's not giving me the outcome that i wanted..
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/16/13 09:15 AM (12 years ago)
(conditiona && condition c) didn't work when i tested it (conditiona && condition b) did work so it's kinda weird. i don't know where i did wrong
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
08/16/13 09:19 AM (12 years ago)
I to find the syntax of logical operators confusing. Sometime I just use an if then an if else statement. if (conditiona && conditionb){ Do something } else if (conditiona && condition c){ Do something } Does the same thing it's more code but normally my if statements lead to another method s writing [self methodOne] twice doesn't bother me
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/16/13 07:11 PM (12 years ago)
thanks kittsy! why didn't i think of that!
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
08/16/13 07:45 PM (12 years ago)
Because th way you're trying to do it is the correct one lol
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/16/13 09:41 PM (12 years ago)
tested it and got the same weird result. i guess i have to let you guys know the specifics because i cant figure it out myself anymore lol i have two nsuserdefaults keyword - one for the full version, and one for the single quiz feature. here's my code if ((levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"unlockquiz"]) || (levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"fullversion"])) the levelLock is just a BOOL variable that i set in buzztouch whether or not i want to lock my screen or not (my app is set to autorefresh everytime it is opened) now for the other condition, the '!' sign makes it negative right? hmm this can be a little bit complicated to explain so please forgive me. i don't have any code for the nsuserdefault condition except in the upgrade or unlock feature code. here is my code: // gets the keyword from buzztouch NSString *lockKeyword = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"lockkeyword" defaultValue:@""]; // unlock feature [[NSUserDefaults standardUserDefaults] setBool:YES forKey:lockKeyword];
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
08/16/13 09:43 PM (12 years ago)
Not sure I understand what's not working. You said: ******* (conditiona && condition c) didn't work when i tested it (conditiona && condition b) did work so it's kinda weird. i don't know where i did wrong ******* And your if/then check was: ******* if ((conditiona && conditionb) || (conditiona && condition c)) ... ******* This would seem to be working just as expected. (conditiona && conditionb) returns true, and (conditiona && condition c) returns false, and your check only looks to make sure one of those two returns true. Since the first does, then the code should execute. Unless I'm not understanding something?
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/16/13 09:44 PM (12 years ago)
i don't have any code for the nsuserdefault condition except in the upgrade or unlock feature code... maybe that's why the first condition is always positive! this one: (levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"unlockquiz"]) what do you guys think? is it the reason why my second condition does not work? im sorry if it's a bit confusing :D
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
08/16/13 09:52 PM (12 years ago)
sorry - just saw your updated and detailed response. So, your check is: ******* if ((levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"unlockquiz"]) || (levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"fullversion"])) ******* This is equivalent to saying: if (levelLock = true AND unlockquiz bool = false) OR (levelLock = true AND fullversion bool = false), then do something; So, if this isn't working correctly, then it has to be one of your booleans is not returning the value you think it is. Try running code like this: NSLog(@"levelLock = %c", levelLock); NSLog(@"unlockquiz = %c", [[NSUserDefaults standardUserDefaults] boolForKey:@"unlockquiz"]); NSLog(@"fullversion = %c", [[NSUserDefaults standardUserDefaults] boolForKey:@"fullversion"]); if (levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"unlockquiz"]) NSLog(@"first condition true"); if (levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"fullversion"]) NSLog(@"second condition true"); if ((levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"unlockquiz"]) || (levelLock && ![[NSUserDefaults standardUserDefaults] boolForKey:@"fullversion"])) { //some code here } ******************** That way, you can check your debugger to see what each boolean value is and better diagnose where your problem is.
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/16/13 09:53 PM (12 years ago)
you're correct chris and thanks for the response i suspect that it is because i don't have any code for this [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"unlockquiz"]; except in the upgrade or easy in app purchase screen... and that makes this code true: ![[NSUserDefaults standardUserDefaults] boolForKey:@"unlockquiz"] and thus prevents to check the second condition (which is to upgrade the full version) :O i'm kinda confused now and trying my best to explain it as clear as i can
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/16/13 09:56 PM (12 years ago)
okay thanks chris. im just going to rest my brain for a while and then try to implement your instructions
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
08/16/13 09:56 PM (12 years ago)
probably - it's looking to see if that bool is false, and if it's not present it will likely return false. One way to do this is to avoid use of booleans in your NSUserDefaults and instead use NSString objects (which can be set to "YES", "NO" or "TRUE", "FALSE", or whatever). The advantage to this is you can also use the: [BT_strings setPrefString:@"" valueOfPref:@""]; [BT_strings getPrefString:@""]; methods for easy reading and writing to NSUserDefaults
 
awesome123
Aspiring developer
Profile
Posts: 132
Reg: Dec 27, 2011
location unknow...
2,320
like
08/22/13 01:12 AM (12 years ago)
ha! fixed it! i ditched this two logical operators and just used the first one. what i did is i had an if statement in the view will appear method that unlocks the first condition if the pro version is unlocked! :D thanks for the help guys! i wish you all the best
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.