簡體   English   中英

Objective-C:簡單的if x == y ||的問題 y || y命令

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

我有一個字符串(包含一個月),並想檢查它是一個'短'月(根據名稱的長度)還是'月'名稱。 根據名稱的長度,我想設置它的位置:

    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的位置。

我究竟做錯了什么? 任何幫助將非常感謝。

這段代碼

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"]) { 

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

也許你應該使用

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

你也可以用這個。

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 (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* 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;
}

嘗試這個。 希望能幫助到你

if語句中的邏輯運算符對於您所需的操作不正確。

你應該寫的是:

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

if()語句中的原始表達式會導致代碼每次都執行,因為表達式的一部分(例如@“APRIL”|| @“MAY”)會導致布爾值為TRUE。

使用NSStringisEqualToString:compare:方法。

使用適當的比較語法。 應該是這樣的: -

 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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM