how to decode objects in NSmutablearray
I am creating a SQLite database.
To get data from the table I am using this code in appdelegate.m
class:
-(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) {
开发者_如何学Python // 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);
}
I am getting this array in viewcontroller.m
class like this:
MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@",appDelegate.itemsList);
It displays output like this:
(
"<Category: 0x6b355d0>",
"<Category: 0x6b356e0>",
"<Category: 0x6b35790>",
"<Category: 0x6b35830>",
"<Category: 0x6b358d0>",
"<Category: 0x6b35980>",
)
How can I convert this to normal array?
If you could post the header for the Category object it would give us more to go on. but you would probably want to implement the description method on it.
-(NSString *)description{
return [NSString stringWithFormat:@"Name:%@ Quantity:%i",self.name,self.quantity];
}
精彩评论