从NSMutableDictionary检索字符串值

时间:2012-07-10 09:11:16

标签: iphone xcode

我想从数据库加载tableView值,为测试目的而调用database.sqlite。 它包含以下内容:

1月1日

2月2日

......直到......

12月12日

正如您将看到我正在使用FMDB。以下行编译但崩溃:

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

我遇到问题与你在Cocoa中处理Ints和String的不一致方式有关,这是我问题的一部分。此外,虽然有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");

3 个答案:

答案 0 :(得分:1)

rows循环之外声明数组while

将以下行移到while循环上方。

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

答案 1 :(得分: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]; 
}

答案 2 :(得分:0)

您可以将int值转换为字符串格式,

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

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