Iphone 目标C:简单的if x==y | | y | | y命令的问题

Iphone 目标C:简单的if x==y | | y | | y命令的问题,iphone,objective-c,cocoa-touch,if-statement,Iphone,Objective C,Cocoa Touch,If Statement,我有一个字符串(包含一个月),希望检查它是“短”月(就名称的长度而言)还是“月”名。根据名称的长度,我想确定其位置: NSString* dateMonth = @"SEPTEMPER"; if (dateMonth == @"MARCH" || @"APRIL" || @"MAY" || @"JUNE" || @"JULY") { CGRect calendarFrame = CGRectMake(170+100, 8, 200, 50); calendar1 = [

我有一个字符串(包含一个月),希望检查它是“短”月(就名称的长度而言)还是“月”名。根据名称的长度,我想确定其位置:

    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;

}
这种情况应该在ELSE函数中结束,但它不。。。我最终得到了170+100的位置

我做错了什么?如果([dateMonth IsequalString:@“MARCH”]| |[dateMonth IsequalString…

当然,也许你应该用

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

if (dateMonth == @"MARCH" || @"APRIL") {
实际评估为

if ((dateMonth == @"MARCH") || (@"APRIL")) {
由于
@“APRIL”
不是零或零,因此它的计算结果为
TRUE

可以做些什么

  • 步骤1(部分固定)

  • 步骤2(这已经起作用了)
    使用
    isEqual
    方法来比较字符串(或任何其他对象),而不是
    =
    isEqualToString
    也有效,正如其他人所建议的那样

  • 第三步(额外里程)
    通过使用set,您可以使它更可读(更简单)

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

使用适当的比较语法。它应该类似于:-

 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;

}

您不能用这种方式编写代码,这是不正确的语法

if (dateMonth == @"MARCH" || @"APRIL" || @"MAY" || @"JUNE" || @"JULY") 
if (dateMonth == @"MARCH" || dateMonth  == @"APRIL" || dateMonth  == @"MAY" || dateMonth  == @"JUNE" || dateMonth  ==@"JULY")
解决这个问题的正确方法是

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
IsequalString:
compare:
方法

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";

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;

}

if语句中的逻辑运算符对于所需的操作不正确

你应该写的是:

if([dateMonth IsequalString:@“MARCH”]| |[dateMonth IsequalString:@“APRIL”]| |…)


if()
语句中的原始表达式会导致每次执行代码,因为表达式的一部分(例如“APRIL”| |@“MAY”)会导致布尔值为TRUE。

这不是工作代码!!!if(dateMonth=@“MARCH”| dateMonth==@“APRIL”| dateMonth==@“MAY”| dateMonth==@“MAY”| dateMonth==@“MAY”| JUNE”| | dateMonth=@“JULY”)你不能这样写!!!
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;

}