Iphone 如何解码NSmutablearray中的对象
我正在创建一个SQLite数据库 要从表中获取数据,我在Iphone 如何解码NSmutablearray中的对象,iphone,objective-c,sqlite,Iphone,Objective C,Sqlite,我正在创建一个SQLite数据库 要从表中获取数据,我在appdelegate.m类中使用此代码: -(void) readItemsFromDatabaseforTable:(NSString *)tableName { // Setup the database object sqlite3 *database; // Init the animals Array itemsList = [[NSMutableArray alloc] init];
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>",
)
(
"",
"",
"",
"",
"",
"",
)
如何将其转换为普通数组?如果您可以发布类别对象的标题,那么我们可以继续讨论。但您可能希望在其上实现描述方法
-(NSString *)description{
return [NSString stringWithFormat:@"Name:%@ Quantity:%i",self.name,self.quantity];
}
您使用sqlite而不是核心数据有什么原因吗?这很少是正确的选择。(没有什么可以说这是错误的选择。)我只是好奇你的理由是什么。是远程数据库还是个人偏好?希望我的答案能有所帮助。只有受虐狂才会在Objective-C中使用SQLite C API,或者改用。(我修复了你的方法案例;+1)