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;
}
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:.
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?
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;
}
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
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
For loop and if statement
Hi everyone,
i have a following for loop and preeceding to it a if else condition.m using the following code.
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
}
else
{
//some code
}
}
suppose strSelectedProjectName is "aaa"and the arrPrjName contains "aaa" "bbb" "ccc" .. after the first iteration of for loop if condition gets true i.e string "aaa" matches with the string in array list,it should get out of the loop at the second iteration,i.e it should not enter the else condition..
add a break; command
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
break;
}
else
{
//some code
}
}
Use break to exit the iteration loop.
Use the break keyword:
for (...) {
if (condition) {
// do stuff
break;
} else {
// do other stuff
}
}
You need to use a break; in your loop to break out of the code if you do not want it to continue to loop.
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
break;//Get out of for loop
}
else
{
//some code
}
}