Problem using a SQLite database on Xcode - iphone

I'm quite new to Xcode and I have a huge problem with a tutorial I'm working on.
I'm trying to write a simple shopping list, using a sql database.
Actually i finished with the project. I triple checked every single line of code, but for some reason it doesn't want to show me the content of the DB nor write something inside.
Really I have no idea what's wrong with the code...
Writing in the DB:
-(IBAction)addShoppingListItem:(id)sender {
//apriamo il database
if (([itemNameField.text length] == 0) || ([priceField.text length] == 0) || ([priceField.text doubleValue] == 0.0)) {
return;
}
sqlite3 *db;
int dbrc; //Codice di ritorno del database (database return code)
DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
dbrc = sqlite3_open(dbFilePathUTF8, &db);
if (dbrc) {
NSLog(#"Impossibile aprire il Database!");
return;
}
//database aperto! Inseriamo valori nel database.
sqlite3_stmt *dbps; //Istruzione di preparazione del database
NSString *insertStatementsNS = [NSString stringWithFormat: #"insert into \"shoppinglist\" (item, price, groupid, dateadded) values (\"%#\", %#, %d, DATETIME('NOW'))", name_field, price_field, [groupPicker selectedRowInComponent:0]];
const char *insertStatement = [insertStatementsNS UTF8String];
dbrc = sqlite3_prepare_v2(db, insertStatement, -1, &dbps, NULL);
dbrc = sqlite3_step(dbps);
//faccio pulizia rilasciando i database
sqlite3_finalize(dbps);
sqlite3_close(db);
// Pulisci i campi e indica successo nello status
statusLabel.text = [[NSString alloc] initWithFormat: #"Aggiunto %#", itemNameField.text];
statusLabel.hidden = NO;
itemNameField.text = #"";
priceField.text = #"";
}
Loading from the database:
-(void)loadDataFromDb {
//apriamo il database
sqlite3 *db;
int dbrc; //Codice di ritorno del database (database return code)
DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate *) [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 dateadded";
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];
//rilascio tutti gli elementi
[dateValue release];
[primaryKeyValue release];
[itemValue release];
[priceValue release];
[groupValue release];
[rowDict release];
}
}
For the brave who wants to check all the project (there is just another class) here there is the link with all my work. http://www.mediafire.com/?qxfde723r29nhtb
Thanks in advance.

You have too many mistakes in your code. Let's briefly look at them:
1) In your application delegate you place db initialization code in applicationDidFinishLaunching method, but according to Apple's manuals:
This method is used in earlier versions of iOS to initialize the
application and prepare it to run. In iOS 3.0 and later, you should
use the application:didFinishLaunchingWithOptions: instead.
so, I just moved initializeDb method into - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2) In ListViewController you have a method loadDataFromDb but I didn't find its usage. You never call this method. In the same controller you have viewWillAppear method that I used to initialize your shoppingListItems array that will be shown in tableView as follows:
-(void)viewWillAppear:(BOOL)animated {
shoppingListItems = [[NSMutableArray alloc] init];
[self loadDataFromDb];
[tableView reloadData];
}
tableView is a outlet to UITableView control that you place in your nib file. I added its declaration in ListViewController.h:
#interface ListViewController : UIViewController {
UITableViewCell *nibLoadedCell;
IBOutlet UITableView *tableView;
}
#property(nonatomic, retain) IBOutlet UITableViewCell *nibLoadedCell;
#end
and bind in interface builder with the actual control.
3) AddItemViewController's method addShoppingListItem didn't work because didn't pass this condition:
if (([itemNameField.text length] == 0) || ([priceField.text length] == 0) || ([priceField.text doubleValue] == 0.0)) {
return;
}
You didn't bind your outlets in interface builder for itemNameField, priceField, groupPicker.
By fixing this I got working solution, but actually I didn't check memory usage and suppose you have also some problem in memory management here.

Related

How to release an NSMutable Array which we need to use in return statements

IM USING SQLITE DATABASE for my iPhone app
In my app in data base to retried the content from the database i used an array and i return this array in a database method for select statement,
for this purpose i allocate an array and i need to release the array,
NSMutableArray *allContacts = [[NSMutableArray alloc] init];
return allContacts;
if i release an array how sh'd i used in return statement
viceversa if i tried to release after return (we can do anything after return)
Any solution please....
How should we use auto release NSMutable array
//Select statement for contacts
//==================================
+ (NSMutableArray*) selectAllContactsFromDB
{
NSString *DBPath = [self copyDBFile];
sqlite3 *contactsDB = nil;
sqlite3_stmt *statement = nil;
NSMutableArray *allContacts = [[NSMutableArray alloc] init];
if (sqlite3_open([DBPath UTF8String], &contactsDB) == SQLITE_OK)
{
NSString *query = [NSString stringWithFormat: #"SELECT ROWID, NAME, MOBILE, FROM CONTACTINFO"];
if(sqlite3_prepare_v2(contactsDB, [query UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
Contact *contact = [[Contact alloc] init];
contact.rowid = sqlite3_column_int(statement, 0);
contact.name = [NSString stringWithUTF8String:(const char*) sqlite3_column_text(statement, 1)];
contact.mobile = [NSString stringWithUTF8String:(const char*) sqlite3_column_text(statement, 2)];
[allContacts addObject:contact];
}
}
else {
NSLog(#"Statement not prepared");
}
}
[DBPath release];
return allContacts;
}
When you return an allocated object from a method pass it as an autoreleased object.
return [allContacts autorelease];
When you get an autoreleased object you need to retain it for further use:
So change the calling method like;
NSMutableArray *temp = [[YourClass selectAllContactsFromDB] retain];
try like this
NSMutableArray *allContacts = [[[NSMutableArray alloc] init] autorelease];
and also like this..
return [allContacts autorelease];
You can write return statement
return [allContacts autorelease];
OR you can use ARC in your project.
You will have to use autorelease :
return [allContacts autorelease];
This way il will be released the next time the autorelease pool will be flushed. And you have followed the golden rule : For each alloc, copy or retain, you must have a release or autorelease.
use ARC(Automatic Reference Counting) or you have a property as a mutuable array and just return the array...
to get u started on ARC, watch this: http://www.youtube.com/watch?v=FxuI4e_Bj58

Save UISwitch state in to sqlite database and access the same for display

I have a form contains text fields,a textView and 2 switches.Now I am using sqlite database for saving data in to database.Every thing is fine with text fields,text view saving,retrieving and even displaying the saved.As we know we don't have any feature called check box,we need to make use of switch which behaves/acts as that of check box functionality.Now while saving the switch state in to the database.I am saving it as a string/text format only,i.e. check the state of switch,assign value as '1' for ON and '0' for OFF to an integer.Now you might get a bit confused how can I save the integer to string format,yes you are right!I am converting the int value in to number and finally number to string.Then I have formed the query and have inserted the values in to sqlite database table.
Here is the code snippet of it for clear understanding:
int smsValue = 0;
if ([smsSwitch isOn])
{
smsValue = 1;
}
else
{
smsValue = 0;
}
NSNumber *smsNum = [NSNumber numberWithInt:smsValue];
NSString *selectedVal = [smsNum stringValue];
The selectedVal is what we are going to pass as string while forming the query,I also have found out that the state is getting saved properly,i.e. the value is correct.
Now I have a requirement,in my form I have switch for sms,Initially the state of that switch is Off,during off state,the last 2 fields i.e. textField and textView are hidden.If the user selects it (i.e. state is ON),then both the fields are said to be open to enter the values.
Totally I have saved 3 reminders,out of which I have saved 2 with out sms and 1 with sms and both the fields(last) filled.Now to pass the data,or to keep track of record saved after filling the form I have used a model class,please see the following snippet for clear understanding...how I have passed values for other fields(textFields):
-(void)viewDidLoad
{
textField.text = reminderInstance.Name;
textField1.text = reminderInstance.Event;
textField2.text = reminderInstance.Date;
textField3.text = reminderInstance.numDays;
textField4.text = reminderInstance.remGroup;
[super viewDidLoad];
}
Here the reminderInstance is object of the model class holding values like name,event,date etc...Now last 2 fields i.e. textField5 and textView are tied or linked to state of sms switch.If it is selected we need to consider both the fields values and pass them for user to edit/make changes.If not we need not bother about the last 2 fields.
Here is the action for the switch:
-(IBAction)toggleEnabledForswitch:(id)sender
{
if(smsSwitch.on)
{
textField5.hidden = NO;
textView.hidden = NO;
}
else
{
textField5.hidden = YES;
textView.hidden = YES;
}
}
Now in save action,I am checking the state of the switch and saving the values accordingly,the following code snippet will show a clear picture:
-(IBAction)save:(id)sender
{
int smsValue = 0;
if ([smsSwitch isOn])
{
smsValue = 1;
}
else
{
smsValue = 0;
}
NSNumber *smsNum = [NSNumber numberWithInt:smsValue];
NSString *selectedVal = [smsNum stringValue];
reminderInstance.smsString = selectedVal;
sqlite3_stmt *statement = nil;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil)
{
if (self.navigationItem.rightBarButtonItem.title == #"Save" && [textField.text length] !=0 && [textField1.text length] !=0 && [textField2.text length] !=0 && [textField3.text length] !=0 && [textField4.text length] !=0)
{
NSLog(#"am in the save loop");
if (smsValue == 0)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO reminders(name,event,date,bfr,grp,val,sms) VALUES (\"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\")", textField.text, textField1.text,textField2.text,textField3.text,textField4.text,isSelected,selectedVal];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);
}
else if (smsValue == 1)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO reminders(name,event,date,bfr,grp,val,sms,num,bod) VALUES (\"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\")", textField.text, textField1.text,textField2.text,textField3.text,textField4.text,isSelected,selectedVal,textField5.text,textView.text];
textField5.text = reminderInstance.number;
textView.text = reminderInstance.msgBody;
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);
}
if (sqlite3_step(statement) == SQLITE_DONE)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"\nReminder Saved" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil,nil];
[alert show];
[alert dismissWithClickedButtonIndex:0 animated:YES];
[alert release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Reminder not saved" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
}
I took another view controller with table view where I have populated a table view to view all the records that are being saved and upon selection we take him/her to the controller page that contains form i.e. populated with all the values.So far I have been successful in passing all the values using:
1.The following code in form controller page:
-(id) initWithReminder:(ReminderClass *)aReminder
{
if ( (self=[super init]) )
{
self.reminderInstance = aReminder;
}
return self;
}
2.The following parts of code in display controller:
-(void)loadReminders
{
// setup the reminders array
self.array = [[NSMutableArray alloc] init];
//Retrieve the values of database
const char *dbpath = [self.databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"SELECT * FROM reminders"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(self.remindersDB ,query_stmt , -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
ReminderClass *loadedReminder = [[ReminderClass alloc] init];
loadedReminder.reminderID = sqlite3_column_int(statement, 0);
loadedReminder.Name = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]autorelease];
loadedReminder.Event = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]autorelease];
loadedReminder.Date = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]autorelease];
loadedReminder.numDays = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)]autorelease];
loadedReminder.remGroup = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)]autorelease];
loadedReminder.selString = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 6)]autorelease];
loadedReminder.smsString = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 7)]autorelease];
NSLog(#"selected value = %#",loadedReminder.smsString);
loadedReminder.number = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 8)]autorelease];
loadedReminder.msgBody = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 9)]autorelease];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *theDate = [dateFormat dateFromString:loadedReminder.Date];
NSString *stringDate = [dateFormat stringFromDate:theDate];
loadedReminder.Date = stringDate;
NSLog(#"Date = %#",loadedReminder.Date);
[self.array addObject:loadedReminder];
[loadedReminder release];
}
sqlite3_finalize(statement);
}
sqlite3_close(self.remindersDB);
}
}
Now what is happening is since I have the table view in display controller with 2 containing fields except that are linked to sms state and one with all the fields filled.When I select the view controller it is crashing at line "loadedReminder.number":
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSPlaceholderString initWithUTF8String:]: NULL cString'
when I remove loadedReminder.number and loadedReminder.msgBody,we can view the table(controller page) no crash problem what so ever...Upon selection of table view cell I am doing the following:
-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReminderClass *rem = [self.array objectAtIndex:indexPath.section];
// Instantiate your detail/editor view controller,
// and pass in the ReminderClass object to be edited.
ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
[self.navigationController pushViewController:rdvc animated:YES];
rdvc.navigationItem.rightBarButtonItem.title = #"Edit";
}
Now I can see all the values properly populated in fields,except the problem with switch.Because it is saving properly,but the switch is always in off state,the state set to default as I have already said during filling the form and saving it (if user selects it and fills the last 2 fields also) to save it(record).
Sorry for this huge description,just wanted to make it detailed,so that any one can understand what my issue is,I have made several searches.But unable to achieve the task.
Can any one please suggest me a way of how to save the switch state in database and then populate the same when loading records from database sqlite.
Thanks every one in advance :)
I found out the solution for it,i.e. use an NSString and assign string value i.e. the retrieved state values that were added to the array.Let me explain this in detail:
In Add Reminder page,take an NSString let's say currentState,now come back to where the saved reminders are viewed,take an array named "stateValues" and add the state values to that array i.e.:
loadedReminder.smsState = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 7)]autorelease];
[stateValues addObject:loadedReminder.smsState];
Now in did select row at index path,where we are navigating to add reminder page for editing or modifying the existing values of reminder,do this:
-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReminderClass *rem = [self.array objectAtIndex:indexPath.section];
NSLog(#" switch state in did : %#",[stateValues objectAtIndex:indexPath.section]);
ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
rdvc.currentState = [stateValues objectAtIndex:indexPath.section];
}
We have successfully set the values to current state,now get back to add reminder page to set the state of switch,i.e. in viewDidLoad method do the following:
-(void)viewDidLoad
{
if ([currentState isEqualToString:#"0"])
{
[smsSwitch setOn:NO animated:NO];
}
else if([currentState isEqualToString:#"1"])
{
[smsSwitch setOn:YES animated:YES];
}
}
That's it,we have now set the state of switch properly by proper access of values that were saved for all consecutive reminders.
Hope it helps.Thank you!

iphone: how to display sqlite data from diff. statement of query in diff. view?

I was able to get a list of title of the books (tableview) and when i select the book i would like to push to the new view of the details of the books. Do i do it in titleView class 'didSelectRow' or in DetailView class 'viewWillAppear' and if so what exactly do I have to put to get the statement, 3 or contentInfo?
Database Class
(NSArray *) itemsByAuthorID:(NSInteger)authorID {
NSMutableArray *retval = [[NSMutableArray alloc] init];
NSString *query;
code....
query = [NSString stringWithFormat:#"select * from books where books.author_id = '%i'", authorID];
}
code...
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK){
while (sqlite3_step(statement) == SQLITE_ROW) {
int itemID = sqlite3_column_int(statement, 0);
int authorID = sqlite3_column_int(statement, 1);
char *nameChars = (char *) sqlite3_column_text(statement, 2);
char *itemContent = (char *) sqlite3_column_text(statement, 3);
NSString *contentTitle = [[NSString alloc] initWithUTF8String:nameChars];
NSString *contentInfo = [[NSString alloc] initWithUTF8String:itemContent];
Item *info = [[Item alloc] initWithItemID:itemID authorID:authorID contentTitle:(NSString *)contentTitle contentInfo:contentInfo];
[retval addObject:info];
}
sqlite3_finalize(statement);
}
return retval;
You can create a private class for Book and retrive data from database for all books in array of Book.
Then after when didSelectRow is called only pass object of Book as parameter and show details in Detail view.

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, 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];
}
}
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.

Strange behavior with NSObject Properties

I have an NSObject that I am using to store/hold properties of an object, one of them being a "Name" property cast as an NSString. I am also pulling data from a SQLite DB for said object using the following:
- (void) getDataToDisplay:(NSString *)dbPath {
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *queryOnStyle = [NSString stringWithFormat:
#"SELECT WineId, Name FROM wine WHERE StyleId = %d", dataManager.styleId];
const char *sql = [queryOnStyle UTF8String];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
Wine *w = [[Wine alloc] init];
w.wineId = sqlite3_column_int(selectstmt, 0);
w.wineName = [NSString stringWithUTF8String:
(char *)sqlite3_column_text(selectstmt, 1)];
[dataManager.wines addObject:w];
[w release];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
Wine being my object. If I were to log w.wineName at this point, there is no problem. The problem occurs later when I try to access the properties of the object from within the array, dataManager.wines, in a custom tableView. It suddenly treats my wineName as a UIImageView rather than an NSString...
I for the life of me cannot trace anything back to ever being cast as a UIImageView and have no idea why it would be setting just that property as such. Here is the code for my custom tableView:
#pragma mark -
#pragma mark HorizontalTableViewDelegate methods
- (NSInteger)numberOfColumnsForTableView:(HorizontalTableView *)tableView {
return [dataManager.wines count];
}
- (UIView *)tableView:(HorizontalTableView *)aTableView viewForIndex:(NSInteger)index {
UIView *vw = [aTableView dequeueColumnView];
if (!vw) {
[[NSBundle mainBundle] loadNibNamed:#"ColumnView" owner:self options:nil];
vw = self.columnView;
self.columnView = nil;
}
// Get the wineId from the array of wineId integers
Wine *w = [dataManager.wines objectAtIndex:index];
int tempWineId = w.wineId;
NSString *tempWineName = [NSString stringWithFormat:#"%#", w.wineName];
NSLog(#"%#", tempWineName); \\RETURNS TEMPWINENAME AS A UIIMAGEVIEW
[w release];
return vw;
}
- (CGFloat)columnWidthForTableView:(HorizontalTableView *)tableView {
//TODO: This value needs to change if changed in IB
return 209.0f;
}
any ideas?
Resolved in comments of question by RichB:
is w.wineName a retain'ed property ? This sounds a bit like the string is being auto released. Could you post the definition of the Wine object?