If-else not executing block when bool is true? - objective-c

This could just as easily be something stupid I missed, but take a look at this code: (which has been stripped down to just debug functionality, although the actual method name was left unchanged)
-(BOOL)shouldHideStatusBarItem:(BOOL)showItem{
if (showItem == YES) {
NSBeep();
NSLog(showItem ? #"YES(inloop)" : #"NO(inloop)" );
}
else if (showItem == NO){
NSBeep();
NSLog(showItem ? #"YES(inloop)" : #"NO(inloop)" );
}
NSLog(showItem ? #"YES" : #"NO" );
return showItem;
}
When I pass in YES it logs:
YES
When I pass in NO it logs:
NO(inloop)
NO
Obviously when I pass in YES it SHOULD log:
YES(inloop)
YES
Does anybody have any ideas?

Change your code to just check the truth of the showItem variable. The BOOL type is not actually restricted to the values YES and NO.
if (showItem)
{
...
}
else
{
...
}

a variation on lakesh's answer
-(BOOL)shouldHideStatusBarItem:(BOOL)showItem{
if (showItem) {
} else {
}
NSBeep();
NSLog(showItem ? #"YES" : #"NO" );
return showItem;
}

Related

Return Statement in Objective C for Binary Search Tree Search Function

I want to write a search function for a Binary Search Tree. How can I write it so that I can return from the base case if(root.data == node.data){return node;} without getting compiler warnings about the method returning nothing. In this case, root is an ivar that's part of the BST class that these methods are a part of.
-(Node*)search:(Node*)node{
if(root == nil){
return node;
}
if(root.data == node.data){
return node;
}
if (node.data < root.data){
root = root.left;
[self search:root];
}
if (node.data > root.data) {
root = root.right;
[self search:root];
}
}
You get the error because you don't return anything from your search: method under certain conditions. You must return something. You also don't make any use of the return value when you recursively call search:.
You also have a problem with updating root. You don't want to do that.
You probably want something more like this:
- (Node *)search:(Node *)node {
if (root == nil) {
return node;
}
if (root.data == node.data) {
return node;
} else if (node.data < root.data) {
return [self search:node.left];
} else // node.data > root.data
return [self search:node.right];
}
}
if none of your first two if tests are true, your method doesn't return anything.
Try;
if (node.data < root.data){
root = root.left;
return [self search:root];
}
if (node.data > root.data) {
root = root.right;
return [self search:root];
}
return ? // you need a return value here

if statement in objective-c [duplicate]

This question already has answers here:
What does the question mark and the colon (?: ternary operator) mean in objective-c?
(13 answers)
Closed 8 years ago.
Can someone please help to explain the syntax of the following code for me? It meant to "return ? if _suit is nil, and return a corresponding string in an array if _suit is not nil".
- (NSString *)suit
{
return _suit ? _suit : #"?";
}
Is it equivalent to the following code?
if (!_suit) {
return #"?";
} else {
return ?
}
Yes, this is a shortening of an if block. It is a conditional operator.
The format is as follows (same in many other languages):
condition ? ifTrue: ifFalse;
So your code:
return _suit ? _suit : #"?";
Is the same as
if(_suit) {
return _suit;
} else {
return #"?";
}
You can read more about it here.
No it is not the same. The '?:' operator describes following it is just an if else statement as one-liner:
(if clause) ? : .
so in your case that would mean:
if (!_suit) {
return #"?";
} else {
return _suit;
}

isEqualToString always returns true

for (Annotation *ann in annotaionArray) // annotationArray contains annotations added to map
{
NSString *fetchedtitle = ann.title;
if([fetchedtitle isEqualToString:oldTitle]); // oldTitle = textfield.text
{
ann.title = appDelegate.pinTitle;
break;
}
}
But the comparison is always true. What could be the error please?
fetched const char from sqlite is casted to stringWithUTF8String.
Everything has been done to cast perfectly to string but still why is the error in comparison?
Remove the trailing semi-colon!
if ([fetchedtitle isEqualToString:oldTitle])
{
ann.title = appDelegate.pinTitle;
break;
}
With the semi-colon, your code is the same as:
if ([fetchedtitle isEqualToString:oldTitle])
;
{
ann.title = appDelegate.pinTitle;
break;
}
Since you have a semi colon at the end of your IF statement your code is :
if( [fetchedtitle isEqualToString:oldTitle])
{
// Do nothing
}
// this will always run
ann.title = appDelegate.pinTitle;
break;
replace the following line
if([fetchedtitle isEqualToString:oldTitle]);
with
if([fetchedtitle isEqualToString:oldTitle])
Just remove semicolon(;) from the following part of your code...
if([fetchedtitle isEqualToString:oldTitle]);

how to check title of uibutton

NSString *title=btn.titleLabel.text;
NSLog(#"Title=%#",title);
if(title == #"SelectCategory")
{
//alert
}
else
{
//somecode
}
I want to check title of UIButton. But my code always executing else statement.
What is the error in this code?
Never compare two strings using '==', use isEqualToString
if ([title isEqualToString:#"SelectCategory"]){
//alert
}else{
//somecode
}
Try this line:
If([btn.titleLabel.text isEqualToString:#"Your text"])
{
//do this
}
else
{
//do this
}
NSString *title=[btn currentTitle];
if([title isEqualToString:#"SelectCategory"])
{
NSLog(#"Equal");
}
Use
if ([title isEqualToString:#"SelectCategory"]) {}
instead of == operator.
Use bun.title is Equalto:#"" it will work for you.
Welcome
Never use == to compare strings, with == you're checking if the pointer of a string is the same of another string, and it's not what you want.
Try this
UIButton *YourButton=btn;
if([[YourButton titleForState:UIControlStateNormal] isEqualToString:#"SelectCategory"])
{
// normal
}
else if([[YourButton titleForState:UIControlStateHighlighted] isEqualToString:#"SelectCategory"])
{
//highlighted
}
else if([[YourButton titleForState:UIControlStateSelected] isEqualToString:#"SelectCategory"])
{
//selected
}
else
{
//somecode
}
Two strings or two objects cannot be compared using ==. To compare two objects you should use isEqual.
In this case :
if([stringToBeCompared isEqualToString:#"comparestring"])
{
//statement
}

Cocos2d - compare two ccColor3B struct colors

I'm working on a game (Cocos2d + Obj-C) where I need to check if two colliding sprites has the same color or not. I've tried the following already:
if (ship.imageSprite.color == base.imageSprite.color)
{
{
NSLog(#"matching colors");
}
}
But I get compile time error: "invalid operands to binary expresson ('ccColor3B'(aka 'struct _ccColor3B') and 'ccColor3B')." What is the way to test two colors? Thanks.
-(BOOL)isccColor3B:(ccColor3B)color1 theSame:(ccColor3B)color2{
if ((color1.r == color2.r) && (color1.g == color2.g) && (color1.b == color2.b)){
return YES;
} else {
return NO;
}
}
You'll have to test the ccColor3B components individually:
ccColor3B col1 = ship.imageSprite.color;
ccColor3B col2 = base.imageSprite.color;
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
{
NSLog(#"matching colors");
}