Scala匹配语句错误

时间:2017-12-12 18:12:36

标签: scala match

我写了以下简单函数来演示if条件的匹配。 它适用于正数,例如evenOdd(5),evenOdd(6)工作正常。但是如果我调用evenOdd(-5),我就会得到MatchError 添加变量模式case _

后,它就消失了

为什么会这样?

def evenOdd(num: Int):Unit = {
    num match {
        case n if (n % 2 == 0) => println("even")
        case n if (n % 2 == 1) => println("odd")
    }
}
scala.MatchError: -5 (of class java.lang.Integer)
at Main$$anon$1.evenOdd(Recursion.scala:17)
at Main$$anon$1.<init>(Recursion.scala:24)
at Main$.main(Recursion.scala:1)
at Main.main(Recursion.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

1 个答案:

答案 0 :(得分:3)

因为-5%2 = -1

因此存在匹配错误,因为此案例未得到处理。

您必须添加(如您所说)

    PFFile *childsPicture = [object objectForKey:@"picture"];
[childsPicture getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
        UIImage *largePicture = [UIImage imageWithData:imageData];
        UIImage *scaledPicture = [Utility scaleMaintainAspectRatio:largePicture :70.0 :70.0 ];
        PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
        thumbnailImageView.image = scaledPicture;
        [self.tableView reloadData];
    }
}];

case _ => println("not treated")