как декодировать объекты в NSmutablearray - PullRequest
0 голосов
/ 17 марта 2011

Я создаю базу данных SQLite.

Для получения данных из таблицы я использую этот код в appdelegate.m классе:

-(void) readItemsFromDatabaseforTable:(NSString *)tableName {
    // Setup the database object
    sqlite3 *database;

    // Init the animals Array
    itemsList = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        NSString *sql_str = [NSString stringWithFormat:@"select * from %@", tableName];

        const char *sqlStatement = (char *)[sql_str UTF8String];
        NSLog(@"query %s",sqlStatement);
        //const char *sqlStatement = "select * from allcategories" ;
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSInteger aDescription =(compiledStatement, 2);
                //  NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                // Create a new animal object with the data from the database

                Category *item = [[Category alloc] initWithName:aName Quantity:aDescription];

                // Add the animal object to the animals Array
                [itemsList addObject:item];

                [item release];
            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

}

Я получаю этот массив в viewcontroller.m классе, например:

MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@",appDelegate.itemsList);

Он отображает вывод так:

(
    "<Category: 0x6b355d0>",
    "<Category: 0x6b356e0>",
    "<Category: 0x6b35790>",
    "<Category: 0x6b35830>",
    "<Category: 0x6b358d0>",
    "<Category: 0x6b35980>",
)

Как я могу преобразовать это в обычный массив?

1 Ответ

2 голосов
/ 17 марта 2011

Если бы вы могли опубликовать заголовок для объекта Category, это дало бы нам возможность продолжить. но вы, вероятно, хотели бы реализовать метод описания на нем.

-(NSString *)description{
    return [NSString stringWithFormat:@"Name:%@ Quantity:%i",self.name,self.quantity];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...