0

我想从数据库中加载 tableView 值,用于测试目的 database.sqlite。它填充了以下内容:

1、一月

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");
4

3 回答 3

1

在循环rows外声明数组。while

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

NSMutableArray *rows = [[NSMutableArray alloc] init];
于 2012-07-10T09:21:10.207 回答
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]; 
}
于 2012-07-10T09:30:13.627 回答
0

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

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

NSlog(@"MonthName :%@", MonthName);
于 2012-07-10T09:18:18.733 回答