今天在写switch的时候遇到个奇怪的问题,
case DEFAULT:
UIImage *image1 = [UIImage imageNamed:@"fillcolor_10.png"];
image1 = [image1 convertToScaleWithColor:GRAYCOLOR];
我直接在case 标签后面 声明 并定义一个 UIImage 对象,但是它提示 expected expression before UIImage的错误,搞了半天没明白哪里出了问题,最后上网找发现,像 case这类标签语句后面 你 不能立即放置一个声明语句, 它们 “can only precede a statement”(后面必须紧跟一个statement 不知道怎么翻译这个 statement )也就是说 在 UIImage前面随便加个 statement 就没问题,
case DEFAULT:
; (加了个分号)
UIImage *image1 = [UIImage imageNamed:@"fillcolor_10.png"];
image1 = [image1 convertToScaleWithColor:GRAYCOLOR];
或者你也可以将你的声明用大括号 括起来,
case DEFAULT:
{
UIImage *image1 = [UIImage imageNamed:@"fillcolor_10.png"];
image1 = [image1 convertToScaleWithColor:GRAYCOLOR];
}
总之我觉得这东西有点古怪,写下来提醒下自己
原文地址:http://blog.youkuaiyun.com/imstyle1001/article/details/6431560
本文详细解释了在Swift中使用switch语句时,在case标签后声明和定义UIImage对象会触发错误的原因,并提供了解决方案。通过在case标签后添加分号或使用大括号括起声明语句,可以避免此错误。文章还引用了一个实际的优快云博客链接作为参考。

被折叠的 条评论
为什么被折叠?



