else语句之前的预期表达式

时间:2020-02-13 08:22:08

标签: ios objective-c

我在 else 语句之前说期望的表达式时出错,但是我不知道为什么。我搜索了其他帖子,但找不到解决方法。

- (void)setDeviationSize:(double)newDeviation
{
        if (newDeviation != 0) {
            deviationLayer.lineWidth = 2.0 / newDeviation;
            if (newDeviation * pixelPerMeter * scrollView.zoomScale < 2 * cPointRadius) {
                deviationLayer.hidden = YES;
            } else {
                deviationLayer.hidden = NO;
                deviationLayer.transform = CATransform3DMakeScale(newDeviation, newDeviation, 0);
            }
        } else {
            deviationLayer.hidden = YES;


        } else  <---- EXPECTED EXPRESSION {

            for(LectureModel* lecture in lectures) {
            NSString *title;
            if([lecture.title length] > 30) {
                title = [NSString stringWithFormat:@"%@...", [lecture.title substringToIndex:30]];
            } else {
                title = lecture.title;
            }

            [alert addActionWithTitle:title handler:^(UIAlertAction * _Nonnull action) {
                LectureModel* full = [LectureModel findById:lecture.id];
                if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                    [self showModelInPopover:full];
                } else {
                    [[TransitionManager shared] openModelInCatalog:full];
                }
        }
        [self presentViewController:alert animated:YES completion:nil];
             }
             }
}

我想念什么?

2 个答案:

答案 0 :(得分:1)

就像vadian和luk2302指出的那样,您具有附加了两个else的if语句,因此编译器无法理解第二个else与什么相关并引发错误。

也许您想要类似

if (newDeviation != 0) {
    /* do something */
} else if (someCondition) {
    /* do something different */
} else   { 
   /* do something else */
}

如果这不是您想要的逻辑,请说明您要实现的目标,以便我们为您提供更好的帮助。

答案 1 :(得分:0)

@ user12372692

为了实现您想要的,有两种方法可以解决此问题-

A. if else condition making.
B. using switch case.

方法A:-

if else condition的写法如下:-

if (condition 1) {
    /* do something for condition 1 is true */
} else if (condition 2) {
     /* do something for condition 2 is true */
} else   { 
   /* do something for  both condition 1 and 2 are false. */
}

因此,您的条件应为:-

if (newDeviation != 0) {
    /* do something  */
} else if (OtherCondition) {
     /* do something*/
} else   { 
   /* do something for  both above two conditions are false */
}

B路:-

switch(newDeviation){
   case (newDeviation != 0 ) : {/* Do your work */}; break;
   case (condition 2) : {/* Do your work */}; break;
   case (condition 3) : {/* Do your work */}; break;
}