开发者

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

开发者 https://www.devze.com 2023-02-28 18:00 出处:网络
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 se

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 d开发者_开发问答oes 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.


Use either the isEqualToString: or compare: methods of NSString.


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;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消