2012-07-30 103 views
0

我從SQLite數據庫獲取行並試圖將它們插入到字典中。除了我不斷收到錯誤!我得到的錯誤「Objective-C指針隱式轉換爲'const id *'不允許使用ARC」我知道這意味着當我添加對象到我的字典時不能使用指針。那麼我該如何解決它,以便將這些數組添加到字典中?從數組添加對象到字典

NSArray *keyArray = [[NSMutableArray alloc] init ]; 
    NSArray *valueArray = [[NSMutableArray alloc ] init ]; 
    NSDictionary* dic; 
    NSInteger dataCount = sqlite3_data_count(statement); 
    while (sqlite3_step(statement) == SQLITE_ROW) { 
     @try { 
      for (int i = 0; i < dataCount; i ++) 
      { 
       NSString* key = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)]; 
       NSString *value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)]; 

       if ([value length] == 0) 
       { 
        value = @""; 
       } 

       keyArray = [keyArray arrayByAddingObject:key]; 
       valueArray = [valueArray arrayByAddingObject:value]; 

      } 

     } 
     @catch (NSException *ex) 
     { 
      NSLog(@"%@,%@", [ex name], [ex reason]); 
     } 

     dic= [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray count:[keyArray count]]; 
+2

爲什麼不使用NSMutableArrays?你的錯誤究竟在哪裏? – ohr 2012-07-30 18:00:05

+1

你在哪一行得到錯誤 – 2012-07-30 18:00:08

+0

'0'長度字符串已經是'@「」',並且不需要檢查該值。如果你正在檢查'NULL',那麼你需要檢查'sqlite3_column_text'的值,因爲'stringWithUTF8String:'會在'NULL'值上崩潰。 – Joe 2012-07-30 18:06:31

回答

5

dictionaryWithObjects:forKeys:count:採用C語言風格的陣列,而不是NSArray對象。 dictionaryWithObjects:forKeys:可能會訣竅,但你可能會更好地構建一個可變的字典,因爲你完全繞過NSArray s。

NSDictionary* dic; 
NSMutableDictionary *tmp = [NSMutableDictionary dictionary]; 

for (int i = 0; i < dataCount; i ++) 
{ 
    NSString* key = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)]; 
    NSString *value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)]; 
    [tmp setObject:value forKey:key]; 
} 
dict = tmp; 
+2

'dictionaryWithObjects:<#(NSArray *)#> forKeys:<#(NSArray *)#>'帶NSArrays,會有幫助嗎? – ohr 2012-07-30 18:02:19

+0

@OscarHernandez非常感謝您提出這個問題,我更新了答案。謝謝! – dasblinkenlight 2012-07-30 18:04:40

+0

既然你的答案是正確的,可以挑選它!刪除'[value length]'檢查,因爲它是不必要的。看到我上面的評論。 – Joe 2012-07-30 18:08:06

0

[dicBodyPost setValue:arrContactAddress forKey:@「contactAddress」];