Objective c iOS:what';创建包含多个参数/语句的循环的最佳方法是什么?

Objective c iOS:what';创建包含多个参数/语句的循环的最佳方法是什么?,objective-c,if-statement,Objective C,If Statement,下面是我要做的:我使用NSCalendar和NSDateComponents对象创建一个循环,该循环将基于日期显示文本和图像。因此,在我看来,DIDLOAD是一个相当标准的: NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateCompone

下面是我要做的:我使用NSCalendar和NSDateComponents对象创建一个循环,该循环将基于日期显示文本和图像。因此,在我看来,DIDLOAD是一个相当标准的:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents =
[gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit) fromDate:today];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
然后我从月份和日期中取整数,得到:

if (month == 1 && day ==1)
[do this] //display text
[do this] //display image
这里是我需要帮助的地方:我最初将此创建为if-else构造:

if (month == 1 && day == 1)
[do this]
[do this]
else if (month == 1 && day == 2)
[do this]
[do this]
但由于某种原因,我一添加第二条语句,就会得到一个错误(预期表达式)

所以我把它改成:

if (month == 1 && day == 1)
[do this]
[do this]
if (month == 1 && day == 2)
[do this]
[do this]
但是现在我的第二条语句被调用了,即使if应该返回0


有没有更好的方法使用开关来实现这一点?是否可以将多个表达式作为开关的一部分

您应该使用花括号:

if (month == 1 && day == 1) {
    [do this]
    [do this]
} else if (month == 1 && day == 2) {
    [do this]
    [do this]
}


这里没有循环,只有if语句。忘记将if和else if之间的语句放入代码块{}?我认为OP忘记将它们放入他/她的代码中,而不仅仅是在这篇文章中,因为这与他/她提到的问题相匹配:“我得到一个错误(预期表达式)”和“即使if应该返回0,也会调用我的第二条语句”@sch:Thank。我省略了大括号。到目前为止,我一直在使用的所有示例都没有在if语句中显示它们。
if (month == 1 && day == 1) {
    [do this]
    [do this]
}
if (month == 1 && day == 2) {
    [do this]
    [do this]
}