【问题标题】:Use of undeclared identifier - identifier is in "if statement"使用未声明的标识符 - 标识符在“if 语句”中
【发布时间】:2014-02-05 22:15:16
【问题描述】:

我有一个提醒Use of undeclared identifier 'sql3'。

如果我删除if statement,则没有问题,但当sql3 在if statement 内时,它似乎无法识别。

有办法解决吗?

if ([typedepartie  isEqual: @"ami"]) {

  const char *sql3 = 
        [[NSString stringWithFormat:@"SELECT id 
                                      FROM tabledesquestions 
                                      WHERE pack = ? ORDER BY RANDOM() LIMIT 4"] 
                   cStringUsingEncoding:NSUTF8StringEncoding];

}

listequestionmulti = [[NSMutableArray alloc]init];

sqlite3_stmt *sql1Statement;
if(sqlite3_prepare(database1, sql3, -1, &sql1Statement, NULL) != SQLITE_OK) {
  NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(database1));
}

【问题讨论】:

    标签: objective-c if-statement undeclared-identifier


    【解决方案1】:

    标识符sql3 在花括号内定义。因此,对于那些花括号来说,它纯粹是本地的。因此,当您越过代码的下一部分时,它就不再存在了。

    if ([typedepartie  isEqual: @"ami"]) {
       const char *sql3 = // sql3 is born here...;
    } // and dies here
    

    你想要的更像是这样的:

    char *sql3 = // default value;
    if ([typedepartie  isEqual: @"ami"]) {
        sql3 = // other value;
    } 
    

    【讨论】:

    • ty matt,那么在花括号之后保持 sql3 有效的好方法是什么?
    • 在花括号外面(即之前)定义它。
    • 大括号中发生的事情,留在大括号中! :)))))
    • 我确认你的答案,你! .但我想要 sql3 的不同字符串,具体取决于 typedepartie 的值。你有一个聪明的方法吗?
    • 先定义它并给它一个默认值。 现在执行“typedepartie”测试来更改值。大括号里面可以看到大括号外面,但是大括号外面看不到大括号里面。
    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多