简单的 switch case 问题.... [目标 c]
如果注释 nslog-line,则会出现错误:
语义问题:使用未声明的 标识符“警报”
switch ([[array objectAtIndex:0]intValue]) {
case 2:
NSLog(@"Allergie alarm"); << commenting this, gives me an error!!!
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"blabal"
message: @"balbalb"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
break;
default:
break;
}
if comment the nslog-line, there is an error:
Semantic Issue: Use of undeclared
identifier 'alert'
switch ([[array objectAtIndex:0]intValue]) {
case 2:
NSLog(@"Allergie alarm"); << commenting this, gives me an error!!!
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"blabal"
message: @"balbalb"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
break;
default:
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
为了在
case
中声明新变量,您需要打开一个新作用域。要打开新范围,只需使用大括号即可,正如其他人已经编写的那样。In order to declare a new variable inside a
case
you need to open a new scope. To open a new scope simply use curly braces as others have already written.您正在使用多行 case 语句。您的语句必须包含在
{
和}
中。因此:You are using multiline case statement. Your statements must be enclosed in
{
and}
. Hence:使用以下
编辑:
使用大括号表示
Case
的语句。Use the below
EDIT:
Use Curly brackets for the statement of
Case
.将语句括在
{}
中即可达到目的。Enclose the statements in
{}
does the trick.你不应该在 switch 内声明变量
尝试这种方式
或将其括在大括号中
you should not declare variables inside switch
try this way
or enclose it in the braces
设置警报代表
问候,
夏姆
set delegate of alert
Regards,
Shyam
我认为问题不在于范围之类的事情。问题是当他评论 nslog 语句时,编译器会读取代码,例如
case 2:UIAlertView *alert ....
意味着认为这是 case 2 的参数。
我检查了案例二之后唯一的第一行不应该是变量的声明行,这意味着它们不是范围问题
i think the problem is not scope like thing. the problem is when he comment the nslog statement then compiler read the code some thing like that
case 2:UIAlertView *alert ....
means think this is a parameter of the case 2.
i check this the only first line after case two should not be a declaration line of variable so that means their is not scope problem