我有一个字符串(包含一个月),并希望检查它是“短”月份(根据名称的长度)还是“月份”名称。根据名字的长度,我想设置它的位置:
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的位置。
我做错了什么?非常感谢任何帮助。
答案 0 :(得分:5)
此代码
if (dateMonth == @"MARCH" || @"APRIL") {
实际上被评估为
if ((dateMonth == @"MARCH") || (@"APRIL")) {
由于@"APRIL"
不是零或零,因此评估为TRUE
。
可以做什么
第1步(部分修复)
if ((dateMonth == @"MARCH") || (dateMonth == @"APRIL")) {
第2步(这已经有效)
使用isEqual
方法比较字符串(或任何其他对象)而不是==
。 isEqualToString
也是有效的,正如其他人所说的那样。
第3步(额外英里)
您可以通过使用set来使其更具可读性(并且更简单)。
NSSet *shortMonths = [NSSet setWithObjects:@"MARCH", @"APRIL", @"MAY", nil];
if ([shortMonths containsObject:@"APRIL"]) {
答案 1 :(得分:2)
if ([dateMonth isEqualToString:@"MARCH"] || [dateMonth isEqualToString ...
UIFont *font = [UIFont boldSystemFontOfSize:11.0];
CGSize size = [dateMonth sizeWithFont:font constrainedToSize:CGSizeMake(200, 50)];
答案 2 :(得分:2)
你也可以使用它。
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;
}
答案 3 :(得分:1)
你不能用这种方式编写代码,这是不正确的语法。
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;
}
答案 4 :(得分:1)
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;
}
试试这个。希望它有所帮助
答案 5 :(得分:1)
if语句中的逻辑运算符对于您所需的操作是不正确的。
你应该写的是:
if([dateMonth isEqualToString:@"MARCH"] || [dateMonth isEqualToString:@"APRIL"] || ...)
if()
语句中的原始表达式会导致代码每次都执行,因为表达式的一部分(例如@“APRIL”|| @“MAY”)会导致布尔值为TRUE。 / p>
答案 6 :(得分:0)
使用isEqualToString:
的{{1}}或compare:
方法。
答案 7 :(得分:-2)
使用正确的比较语法。应该是这样的: -
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;
}