将对象存储在数组中

时间:2011-09-07 00:50:12

标签: iphone objective-c database sqlite nsmutablearray

我正在使用数据库,并尝试将表的行存储为数组中的字典。

#import "RootViewController.h"
#import "ProgettoAppDelegate.h"
#import "AddItemViewController.h"
#import "DetailViewController.h"
#include <sqlite3.h>

@implementation RootViewController
@synthesize nibLoadedCell;
@synthesize addItem;

NSNumberFormatter *priceFormatter;
NSDateFormatter *dateFormatter;
NSMutableArray *shoppingListItems; <--i created here...
NSDictionary *editItem;

-(void) loadDataFromDb {
//apriamo il database
sqlite3 *db;
int dbrc; //Codice di ritorno del database (database return code)

ProgettoAppDelegate *appDelegate = (ProgettoAppDelegate *) [UIApplication sharedApplication].delegate;
const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
dbrc = sqlite3_open(dbFilePathUTF8, &db);
if (dbrc) {
    NSLog(@"Impossibile aprire il Database!");
    return;
}
//database aperto! Prendiamo i valori dal database.
sqlite3_stmt *dbps; //Istruzione di preparazione del database
NSString *queryStatementNS = @"select key, item, price, groupid, dateadded from shoppinglist order by item";
const char *queryStatement = [queryStatementNS UTF8String];
dbrc = sqlite3_prepare_v2(db, queryStatement, -1, &dbps, NULL);

//Richiamo la funzione sqlite3_step() finché ho righe nel database
while ((dbrc = sqlite3_step(dbps)) == SQLITE_ROW) {
    int primaryKeyValueI = sqlite3_column_int(dbps, 0);
    NSNumber *primaryKeyValue = [[NSNumber alloc] initWithInt:primaryKeyValueI];
    NSString *itemValue = [[NSString alloc] initWithUTF8String:(char*) sqlite3_column_text(dbps, 1)];
    double priceValueD = sqlite3_column_double(dbps, 2);
    NSNumber *priceValue = [[NSNumber alloc] initWithDouble:priceValueD];
    int groupValueI = sqlite3_column_int(dbps, 3);
    NSNumber *groupValue = [[NSNumber alloc] initWithInt:groupValueI];
    NSString *dateValueS = [[NSString alloc] initWithUTF8String:(char*) sqlite3_column_text(dbps, 4)];
    NSDate *dateValue = [dateFormatter dateFromString: dateValueS];

    NSMutableDictionary *rowDict = [[NSMutableDictionary alloc] initWithCapacity:5];
    [rowDict setObject:primaryKeyValue forKey: ID_KEY];
    [rowDict setObject:itemValue forKey: ITEM_KEY];
    [rowDict setObject:priceValue forKey: PRICE_KEY];
    [rowDict setObject:groupValue forKey: GROUP_ID_KEY];
    [rowDict setObject:dateValue forKey: DATE_ADDED_KEY];

    [shoppingListItems addObject: rowDict];
    NSLog(@"%d", [shoppingListItems count]); //I have a Breakpoint here!

    //rilascio tutti gli elementi
    [dateValue release];
    [primaryKeyValue release];
    [itemValue release];
    [priceValue release];
    [groupValue release];
    [rowDict release];
}
}

在程序结束时使用断点,我可以看到变量中有数据库的内容,但数组“shoppingListItems”为空。 (count = 0)

如果你有足够的勇气去看看,这里有整个项目:http://cl.ly/9uvb

4 个答案:

答案 0 :(得分:1)

您尚未显示shoppingListItems的定义,但添加到数组时有两个常见问题:

  1. 数组是NSArray而不是NSMutableArray,因为它必须是
  2. 数组为零 - 您可能使用[NSMutableArray数组]创建它而没有显式保留?
  3. 是的,检查了你的代码 - 你根本就没有初始化它。解决这个问题,你应该没事。

答案 1 :(得分:1)

我在上面的代码中看不到创建数组的任何内容。如果shoppingListItems为零,则那些-addObject:消息不执行任何操作。

答案 2 :(得分:1)

你需要将所有变量声明为实例变量,我的意思是在.h文件中,如下所示

// .h
@interface RootViewController : UITableViewController {
    UITableViewCell *nibLoadedCell;
    AddItemViewController *addItem;
    IBOutlet UITableView *tableView;

    NSNumberFormatter *priceFormatter;
    NSDateFormatter *dateFormatter;
    NSMutableArray *shoppingListItems; // <--- this is only a declaration (not creates the object)
    NSDictionary *editItem;
}

正确初始化对象,viewDidLoad是完成这项工作的好地方:

//。米

- (void)viewDidLoad
{
    [super viewDidLoad];

    shoppingListItems = [NSMutableArray new]; // <---- This create the object
    // other initialization ....

    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        [dateFormatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
    }

    if (! priceFormatter) {
        priceFormatter = [[NSNumberFormatter alloc] init];
        [priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    }
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

您的问题取决于shoppingListItems的零值,也不要忘记在dealloc方法上发布变量

答案 3 :(得分:0)

听起来你实际上并没有真正创建数组进入shoppingListItems,所以它没有。