2011-09-07 227 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

相關問題