简体   繁体   中英

Objective-C: problem with a simple if x == y || y || y command

I have a string (containing a month) and would like to check if it is a 'short' month (in terms of the length of the name) or a 'month' name. According to the length of the name I would like to set up its position:

    NSString* dateMonth = @"SEPTEMPER";
if (dateMonth == @"MARCH" || @"APRIL" || @"MAY" || @"JUNE" || @"JULY") {

    CGRect calendarFrame = CGRectMake(170+100, 8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

} else {

    CGRect calendarFrame = CGRectMake(170,     8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

}

This case should end up in the ELSE function, but it does not... I end up with the 170+100 position.

What am I doing wrong? Any help would be very much appreciated.

This code

if (dateMonth == @"MARCH" || @"APRIL") {

is actually evaluated as

if ((dateMonth == @"MARCH") || (@"APRIL")) {

And since @"APRIL" is not a zero or nil, it's evaluated as TRUE .

What can be done

  • Step 1 (partly fixed)

     if ((dateMonth == @"MARCH") || (dateMonth == @"APRIL")) { 
  • Step 2 (this already works)
    Use isEqual method to compare strings (or any other objects) instead of == . isEqualToString is also valid, as other people suggest.

  • Step 3 (extra mile)
    You can make it much more readable (and simpler) by utilizing set.

     NSSet *shortMonths = [NSSet setWithObjects:@"MARCH", @"APRIL", @"MAY", nil]; if ([shortMonths containsObject:@"APRIL"]) { 

if ([dateMonth isEqualToString:@"MARCH"] || [dateMonth isEqualToString ...

of maybe you should use

UIFont *font = [UIFont boldSystemFontOfSize:11.0];
CGSize size = [dateMonth sizeWithFont:font constrainedToSize:CGSizeMake(200, 50)];

You can also use this.

NSString* dateMonth = @"SEPTEMPER";

NSString* dateMonths = @"MARCH,APRIL,MAY,JUNE,JULY";

if ([dateMonths rangeOfString:dateMonth].location != NSNotFound){

    CGRect calendarFrame = CGRectMake(170+100, 8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

} else {

    CGRect calendarFrame = CGRectMake(170,     8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

}

You can't write code in this way this is not right syntax.

if (dateMonth == @"MARCH" || @"APRIL" || @"MAY" || @"JUNE" || @"JULY") 
if (dateMonth == @"MARCH" || dateMonth  == @"APRIL" || dateMonth  == @"MAY" || dateMonth  == @"JUNE" || dateMonth  ==@"JULY")

The right solution for this problem is follow ...

NSString* dateMonth = @"SEPTEMPER";
    if ([dateMonth isEqualToString:@"MARCH"] || [dateMonth isEqualToString:@"APRIL"] || [dateMonth isEqualToString:@"MAY"] || [dateMonth isEqualToString:@"JUNE"] || [dateMonth isEqualToString:@"JULY"]) {

    CGRect calendarFrame = CGRectMake(170+100, 8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

} else {

    CGRect calendarFrame = CGRectMake(170,     8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

}
NSString* dateMonth = @"SEPTEMPER";

if ([dateMonth isEqualToString:@"MARCH"] || [dateMonth isEqualToString:@"APRIL"] || [dateMonth isEqualToString:@"MAY"] || [dateMonth isEqualToString:@"JUNE"] || [dateMonth isEqualToString:@"JULY"]) {
CGRect calendarFrame = CGRectMake(170+100, 8, 200, 50);
calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
calendar1.clipsToBounds = true;
} 
else
{
CGRect calendarFrame = CGRectMake(170,     8, 200, 50);
calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
calendar1.clipsToBounds = true;
}

try this. Hope it helps

Your logical operator inside the if statement is incorrect for your desired operation.

What you should write is something like:

if([dateMonth isEqualToString:@"MARCH"] || [dateMonth isEqualToString:@"APRIL"] || ...)

Your original expression inside the if() statement would have caused the code to execute every time because part of the expression (for example @"APRIL" || @"MAY") would result in a boolean TRUE.

使用NSStringisEqualToString:compare:方法。

Use proper comparison syntax. It should be like :-

 NSString* dateMonth = @"SEPTEMPER";

if (dateMonth == @"MARCH" || dateMonth == @"APRIL" || dateMonth == @"MAY" || dateMonth == @"JUNE" || dateMonth == @"JULY")

{
    CGRect calendarFrame = CGRectMake(170+100, 8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

} else {

    CGRect calendarFrame = CGRectMake(170,     8, 200, 50);
    calendar1 = [[UIView alloc] initWithFrame:calendarFrame];
    calendar1.clipsToBounds = true;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM