I'm trying to create a conditional statement that returns to me "nothing entered" if the string length is less than 1. However, I'm getting an error message in xcode that says: Expected identifier. I think my syntax may be wrong but I can't figure it out.
- (IBAction)batman:(id)sender {
if ([self.nameTextField.text.length] < 1)
{ //returnType method argument
[NSString stringWithFormat:#"nothing entered."];
}
else {
self.secondLabel.text = [NSString stringWithFormat:#"Batman %#", self.nameTextField.text ];
}
}
Just get rid of your brackets [] and it should work:
- (IBAction)batman:(id)sender {
if (self.nameTextField.text.length < 1) {
self.secondLabel.text = #"nothing entered.";
} else {
self.secondLabel.text = [NSString stringWithFormat:#"Batman %#", self.nameTextField.text ];
}
}
Related
Little explanation of the code : I have a dict that looks like this :
HC_1 : test1, HC_2: test2, ....
If I find HC_1 in the "theModifiedReportTemplateContent", I look for the first ">" after and I add "test1".
-(NSMutableString*)almMapData:(NSDictionary *)aDictData;
{
NSMutableString *theModifiedReportTemplateContent = itsReportTemplateContent mutableCopy];
for(id theCell in aDictData)
{
NSRange theNameRange = [theModifiedReportTemplateContent rangeOfString:theCell];
NSString *theNewContent = [theModifiedReportTemplateContent substringFromIndex:theNameRange.location];
if(theNameRange.location != NSNotFound)
{
NSRange theCharRange = [theNewContent rangeOfString:#">"];
long theIndex =((long)theCharRange.location+(long)theNameRange.location+1);
[theModifiedReportTemplateContent insertString:[aDictData objectForKey:theCell] atIndex:theIndex];
}
}
return theModifiedReportTemplateContent;
}
My problem is that we I print theCharRange, I have something like :
location=13, length=1
{
2001-01-01 00:00:00 +0000
<object returned empty description>
}
So I have a exception with InsertString atIndex.
I think the following code speaks for itself.
if (card==1) {
cardImageString = #"myGrabbedImage1.png";
} else if (card==2) {
cardImageString = #"myGrabbedImage2.png";
} else if (card==3) {
cardImageString = #"myGrabbedImage3.png";
} else if (card==4) {
cardImageString = #"myGrabbedImage4.png";
} else if (card==5) {
cardImageString = #"myGrabbedImage5.png";
} else if (card==6) {
cardImageString = #"myGrabbedImage6.png";
} else if (card==7) {
cardImageString = #"myGrabbedImage7.png";
} else if (card==8) {
cardImageString = #"myGrabbedImage8.png";
}
The actual if-else is 10 times bigger than this.
Card = int.
CardImageString = NSString.
'card' is only used in this statement and can be removed in your answer.
How can I simplify all this??? Thanks!!
cardImageString = [NSString stringWithFormat:#"myGrabbedImage%#.png", #(card)];
cardImageString = [NSString stringWithFormat:#"myGrabbedImage%d.png",card];
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]);
const char *sentence = "He was not in the cab at the time.";
printf("\"%s\" has %d spaces\n", sentence, (int) ^ {
int i = 0;
int countSpaces = 0;
while (sentence[i] != '\0') {
if (sentence[i] == 0x20) {
countSpaces++;
}
i++;
}
return countSpaces;
});
This code simply counts the white space in a string, but for some reason it says 1606416608 spaces rather than 8. I'm not exactly sure what is going wrong, so thanks for any help!
You're passing the actual block to printf, not the result of the block. Instead, try
const char *sentence = "He was not in the cab at the time.";
printf("\"%s\" has %d spaces\n", sentence, (int) ^ {
int i = 0;
int countSpaces = 0;
while (sentence[i] != '\0') {
if (sentence[i] == 0x20) {
countSpaces++;
}
i++;
}
return countSpaces;
}()); // <-- note the extra parentheses here, indicating that you're calling the block
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
}