Storing objects in an Array
I'm working with a database, and trying to store the rows of a table as dictionary in an Array.
#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, 开发者_StackOverflowitem, 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];
}
}
using Breakpoint at the end of the procedure, i can see that in the variables there are the contents of the database, but array "shoppingListItems" is empty. (count = 0)
If you are brave enough to take a look, here there is the entire project: http://cl.ly/9uvb
You need to declare all your variables as instance variables, i mean in the .h file as shown below
// .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;
}
And correctly initialize the objects, viewDidLoad is a good place to do this work:
//.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;
}
Your problem resides on a nil value for shoppingListItems
also dont forget to release the variable on your dealloc
method
You have not shown the definition of shoppingListItems but there are two common problems when adding to an array:
- The array is an NSArray and not a NSMutableArray as it must be
- The array is nil - you may have created it using [NSMutableArray array] without explicit retain?
Yeah, checked your code - you never initialise it at all. Fix that and you should be OK.
I don't see anything in your code above that creates the array. If shoppingListItems
is nil, then those -addObject:
messages do nothing.
It sounds like you aren't ever actually creating the array to go in shoppingListItems
, so it's nil.
精彩评论