2012-07-10 84 views
0

我想從數據庫中加載tableView值,調用test.sqlite。 它填充了以下內容:從NSMutableDictionary中檢索字符串值

1月

2月

...到...

12月

正如你會看到我使用FMDB。下面一行編譯但崩潰:話說

NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"]; 

我有問題,你在可可處理類型爲int和字符串不一致的方式這是我的問題的一部分。另外,雖然在那裏有NSLog調試線,但這種方法看起來很複雜。你可以給我什麼建議?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docsPath = [paths objectAtIndex:0]; 
NSString *path = [docsPath stringByAppendingPathComponent:@"database.sqlite"]; 
FMDatabase *database = [FMDatabase databaseWithPath:path]; 
[database open]; 

NSLog(@"Open Database"); 
FMResultSet *results = [database executeQuery:@"SELECT * FROM monthly"]; 

while([results next]) { 
    NSString *name = [results stringForColumn:@"name"]; 
    NSInteger age = [results intForColumn:@"age"]; 

    // Stuff Data into Array 
    NSMutableArray *rows = [[NSMutableArray alloc] init]; 
    NSMutableDictionary *firstRow = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:age], @"MonthID", [NSString stringWithFormat:@"%@", name], @"MonthName", nil]; 
    [rows addObject:firstRow]; 
    NSLog(@"Month: %@ - %d",name, age); 
    int myNumber = [[firstRow objectForKey:@"MonthID"] intValue]; 

    // *** THE LINE BELOW IS GIVING ME PROBLEMS *** 
    NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"]; 

    // NSLog(@"Month No: %@ - %d",myString, myNumber); 
    NSLog(@"***** Month No: %d", myNumber); 
    // [self.monthMonths myString]; 
} 
[database close]; 
NSLog(@"Close Database"); 
+0

您的項目崩潰的唯一原因是因爲對於每個循環迭代創建行陣列,當你插入的對象時,它也適用於指數0。因此,每次數組中只有1個對象。正如@Aadhira所說,向上移動線路,希望你會沒事的。 – 2012-07-10 09:29:12

回答

1

此行給出錯誤,因爲你的食指或一個月ID是從1開始的,但是當你在陣列添加:

[rows addObject:firstRow]; 

它在0指數(默認起始指數)被添加。 所以,當你調用這個行:

NSString *myString = [[row s objectAtIndex:myNumber] objectForKey:@"MonthName"]; 

它試圖索引1處提取的價值,因爲:

int myNumber = [[firstRow objectForKey:@"MonthID"] intValue]; 

它實際上嘗試提取索引1值中不存在,因爲您的陣列只包含索引0處的1值使其崩潰。

編輯:嘗試使用這樣的:

NSMutableArray *rows = [[NSMutableArray alloc] init]; 
while([results next]) { 
    NSString *name = [results stringForColumn:@"name"]; 
    NSInteger age = [results intForColumn:@"age"]; 

    NSMutableDictionary *firstRow = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:age], @"MonthID", [NSString stringWithFormat:@"%@", name], @"MonthName", nil]; 

// Stuff Data into Array 
    [rows addObject:firstRow]; 
    NSLog(@"Month: %@ - %d",name, age); 
    int myNumber = [[firstRow objectForKey:@"MonthID"] intValue]-1; //to get value at proper index... 

    // *** THE LINE BELOW IS GIVING ME PROBLEMS *** 
    NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"]; 

    // NSLog(@"Month No: %@ - %d",myString, myNumber); 
    NSLog(@"***** Month No: %d", myNumber); 
    // [self.monthMonths myString]; 
} 
+0

感覺尷尬關於在一個循環內聲明數組,並且是的,我確實需要通過-1來抵消該數組,否則它會崩潰。兩個寶貴的經驗教訓。感謝您的時間和耐心。 – 2012-07-10 12:35:12

+0

歡迎您! – 2012-07-10 12:37:57

0

則可以將int值轉換爲字符串格式,

int myNumber = [[firstRow objectForKey:@"MonthID"] intValue]; 
NSString *myString = [NSString StringWithFormat:@"%d",[[rows objectAtIndex:myNumber] objectForKey:@"MonthName"]]; 

NSlog(@"MonthName :%@", MonthName); 
1

聲明數組rowswhile循環之外。

將以下行移動到while循環上方。

NSMutableArray *rows = [[NSMutableArray alloc] init];