在for循环中使用时,NSArray会崩溃我的程序

时间:2015-12-09 03:48:35

标签: objective-c arrays multidimensional-array nsmutablearray nsarray

我正在使用SQLite3从数据库中获取数据。所以我试图创建一个Multidimensional数组来保存所有记录的数组,这些记录是数组中的嵌套数组。所以当循环到达这段代码时:

[[studentsInfo objectAtIndex:recordRow] addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, studentID, studentEmail, programName, majorityCampus, discipline, cmName, cmID, cmEmail, cmPhone, cmCampus, studentNotes, nil]];
它崩溃了。这是我从Xcode获得的。

Snapshot

这是完整的代码我做错了什么?

    - (void) initializeDatabase {

    // get the documents directory
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = dirPaths[0];
    NSLog(@"Directory Path: %@", docsDir);

    FindYourMentorDB_Path = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"FindYourCM_Info.sqlite"]];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:FindYourMentorDB_Path] == NO) {

        // attempt to open the db
        if (sqlite3_open([FindYourMentorDB_Path UTF8String], &_FindYourMentorDB) == SQLITE_OK){

            sqlite3_close(_FindYourMentorDB); // Close the DB

            NSLog(@"Succeeded in opening FindYourMentorDB");
        } else {

            NSLog(@"Failed to Open the FindYourMentorDB");
        } // END OF IF

    } // END OF IF

    NSMutableArray *studentsInfo = [NSMutableArray new];

    sqlite3_stmt *statement;

    if (sqlite3_open([FindYourMentorDB_Path UTF8String], &_FindYourMentorDB) == SQLITE_OK) {

        NSString *selectSQL = [NSString stringWithFormat:@"SELECT StudentLastName, StudentFirstName, StudentID, StudentEmail, ProgramName, MajorityCampus, Discipline, CMName, CMID, CMEmail, CMPhone, CMCampus, StudentNotes FROM FindYourCM_DB"];

        const char *selectStmt = [selectSQL UTF8String];

        if (sqlite3_prepare_v2(_FindYourMentorDB, selectStmt, -1, &statement, NULL) == SQLITE_OK) {

            for (int recordRow = 0; sqlite3_step(statement) == SQLITE_ROW; recordRow++) {

                NSString *studentLastName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement,0)];

                NSString *studentFirstName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];

                NSString *studentID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 2)];

                NSString *studentEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 3)];

                NSString *programName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 4)];

                NSString *majorityCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 5)];

                NSString *discipline = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 6)];

                NSString *cmName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 7)];

                NSString *cmID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 8)];

                NSString *cmEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 9)];

                NSString *cmPhone = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 10)];

                NSString *cmCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 11)];

                NSString *studentNotes = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 12)];

                [[studentsInfo objectAtIndex:recordRow] addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, studentID, studentEmail, programName, majorityCampus, discipline, cmName, cmID, cmEmail, cmPhone, cmCampus, studentNotes, nil]];
            }

            sqlite3_finalize(statement);
        }

        sqlite3_close(_FindYourMentorDB);
    }
}

3 个答案:

答案 0 :(得分:0)

主要问题是这一行:

[[studentsInfo objectAtIndex:recordRow] addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, ...

由于studentsInfo是一个新的空数组,因此无法从中访问它。您只想添加如下:

[studentsInfo addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, ...

还有一些其他事情可以清理。

- (void) initializeDatabase {
    // get the documents directory
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = dirPaths[0];
    NSLog(@"Directory Path: %@", docsDir);

    findYourMentorDB_Path = [docsDir stringByAppendingPathComponent:@"FindYourCM_Info.sqlite"];

    NSMutableArray *studentsInfo = [NSMutableArray new];

    if (sqlite3_open([findYourMentorDB_Path UTF8String], &_findYourMentorDB) == SQLITE_OK) {
        NSString *selectSQL = @"SELECT StudentLastName, StudentFirstName, StudentID, StudentEmail, ProgramName, MajorityCampus, Discipline, CMName, CMID, CMEmail, CMPhone, CMCampus, StudentNotes FROM FindYourCM_DB";

        const char *selectStmt = [selectSQL UTF8String];

        sqlite3_stmt *statement;
        if (sqlite3_prepare_v2(_findYourMentorDB, selectStmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                NSString *studentLastName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement,0)];
                NSString *studentFirstName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
                NSString *studentID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 2)];
                NSString *studentEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 3)];
                NSString *programName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 4)];
                NSString *majorityCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 5)];
                NSString *discipline = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 6)];
                NSString *cmName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 7)];
                NSString *cmID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 8)];
                NSString *cmEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 9)];
                NSString *cmPhone = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 10)];
                NSString *cmCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 11)];
                NSString *studentNotes = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 12)];

                [studentsInfo addObject:@[ studentLastName, studentFirstName, studentID, studentEmail, programName, majorityCampus, discipline, cmName, cmID, cmEmail, cmPhone, cmCampus, studentNotes ]];
            }

            sqlite3_finalize(statement);
        } else {
            NSLog(@"Unable to prepare statement: %s", sqlite3_errmsg(_findYourMentorDB));
        }

        sqlite3_close(_findYourMentorDB);
    } else {
        NSLog(@"Unable to open database at %@", findYourMentorDB_Path);
    }
}

的问题:

  1. 填充后,您对studentsInfo数组做了什么?
  2. 使用正确的命名约定 - 变量和方法名称应以小写字母开头。类名以大写字母开头。
  3. 记录错误,以便了解为什么事情无法发挥作用。
  4. 您需要检查数据库文件,然后再进行不必要的打开和关闭。如果数据库文件不存在,则在使用sqlite3_open时默认情况下会创建该文件。

答案 1 :(得分:0)

我明白了。 DB中的StudentNotes为NULL,因此导致崩溃。所以我把代码更改为了。感谢您的帮助。

enter image description here

答案 2 :(得分:-1)

使用下一个方法:insertObject:yourarray atIndex:yourindex