2011-09-07 150 views
1

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

#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”是空的。 (计数= 0)

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

+0

可能重复[NSMutableArray addObject不影响计数?](http://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count)和[NSMutableArray addObject:not working](http: //stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working) –

回答

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中是做这项工作的好地方:

//.m

- (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一个零值,也不要忘记撤销对您变量方法

+0

我会编辑该谢谢 – D33pN16h7

0

这听起来像你是不是曾经实际创建数组中shoppingListItems去的,所以它的零。

+0

我创建了!我在另一个项目中使用这个代码做了同样的事情,但在另一个项目中工作正常,在这里我有这个奇怪的问题... – Oiproks

1

您没有示出shoppingListItems的定义,但加入到一个数组当存在两个共同的问题:

  1. 阵列是一个NSArray而不是NSMutableArray的,因为它必须是
  2. 阵列是nil - 你可能使用[NSMutableArray数组]创建它而没有显式保留?

是的,检查你的代码 - 你永远不会初始化它。解决这个问题,你应该没问题。

+0

它是可变的,我创建为'NSMutableArray * shoppingListItems;' – Oiproks

+0

你定义了它,但没有创建它。直到你给这个指针指定了一些东西(比如shoppingListItems = [[NSMutableArray array] retain]),它是nil和addObject,作为nil的消息,什么都不会做。 –

1

我在上面的代码中没有看到任何东西可以创建数组。如果shoppingListItems是零,那么这些-addObject:消息什么也不做。

+0

该数组是上面创建的NSMutableArray'#include @implementation RootViewController @synthesize nibLoadedCell; @synthesize addItem; NSNumberFormatter * priceFormatter; NSDateFormatter * dateFormatter; NSMutableArray * shoppingListItems;'... – Oiproks

相关问题