I have following code in objective-c:-
- if(![[NSUserDefaults standardUserDefaults]
valueForKey:#"habibi_Gender"] || ![[NSUserDefaults
standardUserDefaults] valueForKey:#"my_Gender"]) { // do something
here }
I want to write this same condition in swift.
Try This
let habibi = NSUserDefaults.standardUserDefaults().defaults.stringForKey("habibi_Gender")
let mygender = NSUserDefaults.standardUserDefaults(). defaults.stringForKey("my_Gender")
if !(habibi != nil || mygender != nil)
{
// do something here
}
Exactly the same
if condition1 || condition2 {
//do something
}
Question was asked already here: Multiple Conditions for Swift 'If' Statement?
Related
This question already has answers here:
Warning: control reaches end of non-void function - iPhone
(2 answers)
Closed 6 years ago.
I want to return "K" when setLocationType is "ABC Office" and return "W" when setLocationType is "ABCDE Office". I am getting "control may reach end of non-void function" error and I am not able to proceed further.
+ (NSString*) retrieveLocationType
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *setLocationType = [prefs stringForKey:locationType];
if (setLocationType == #"ABC Office"){
return #"K";
}
else if (setLocationType == #"ABCDE Office"){
return #"W";
}
}
The error is given by the fact that the compiler finds a structure like the following
if (condition1) { return foo; }
else if (condition2) { return bar; }
And what happens when both condition1 and condition2 are false? No return statement is executed but the function must return a value of type NSString. You must change the statements into something like
if (condition1) { .. }
else if (condition2) { .. }
else { return baz; }
or
if (condition1) { .. }
else { .. }
Mind that comparing NSString with == operator compares just the memory address of the objects, you should use isEqualToString:.
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;
}
I have a question and I have not been able to find an answer.
Is there any way to reduce the following expression in Objective-C?
if ((r != 1) && (r != 5) && (r != 7) && (r != 12)) {
// The condition is satisfied
}else{
// The condition isn't satisfied
}
For example (not working):
if (r != (1 || 5 || 7 || 12)) {
// The condition is satisfied
}else{
// The condition isn't satisfied
}
Thanks!
You can use NSSet, like this:
NSSet *prohibited = [NSSet setWithArray:#[#1, #5, #7, #12]];
if (![prohibited containsObject:[NSNumber numberWithInt:r]]) {
// The condition is satisfied
} else {
// The condition isn't satisfied
}
If the set of numbers contains a fixed group of numbers, such as in your example, you can make the NSSet *prohobited a static variable, and initialize it once, rather than doing it every time as in my example above.
You can also use switch for this like
switch (r)
{
case 1:
case 5:
case 7:
case 12:
// r is having 1,5,7 or 12
break;
default:
// r is having other values
}
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
}
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");
}