2014-02-05 29 views
0

我有一個提醒Use of undeclared identifier 'sql3'使用未聲明的標識符 - 標識符位於「if語句」中

如果我刪除if statement,沒有問題,但看起來在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)); 
} 

回答

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; 
} 
+0

TY無光,有啥保持SQL3有效的大括號後的好辦法嗎? – user3277279

+0

在花括號之外(即之前)定義它。 – matt

+1

大括號裏會發生什麼,留在大括號裏! :))))) – matt