An Objective C Category Item Class and using NSLog with NSInteger EXC_Bad_Access

Hopefully people find this class useful its mainly for my own reference but I ran into a few issues when writing it and learnt alot along the way so I thought others might find it useful.Its just a simple objective c class for a table that is stored in SQLLite database, adding in

It uses the FMDB SQLLite wrapper. Jump over to github I recommend you use it I was previously writing my own code.

When writing this I was overlooking a simple mistake and spent ages trying to figure out why my new getAll method wasn't working it appeared to be retrieving the data from the database but when I went to do something like

NSMutableArray *categoryList = [CategoryItem getAll]; NSLog(@"number of items is %@", [categoryList count]); <<--- %@ should be %i

This was throwing a Program received signal EXC_Bad_Access error on the NSLog statement and for the life of me I couldn't figure out why Turns out you can't use "%@" when printing a NSInteger you have to use %i. Which makes sense right! Since NSInteger is not an objective C class but its on data structure (I'm pretty sure I was thinking of NSNumber as its Objective-C class, a subclass of NSValue to be specific)

Anyway the files are as follows

The CategoryItem.h file is

//  CategoryItem.h
//  Chika
//  Created by Luke on 6/04/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.


@interface CategoryItem : NSObject {
    NSInteger ID;
    NSInteger SortOrder;
    NSString *Name;
    NSString *ShoppingImage;


@property (nonatomic, nonatomic) NSInteger SortOrder;
@property (nonatomic, retain) NSString * Name;
@property (nonatomic, retain) NSString * ShoppingImage;
@property (nonatomic, nonatomic) NSInteger ID;

- (id)initWithObject:(NSInteger)itemID;
+ (NSMutableArray *)getAllSpecial;

The CategoryItem.m file is

#import "CategoryItem.h"

#import "SQLite.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"

@implementation CategoryItem

@synthesize ShoppingImage;
@synthesize Name;
@synthesize ID;
@synthesize SortOrder;

- (id)initWithObject:(NSInteger)itemID {

    if ((self = [super init])) {
        sqlite3 *database;
        // Open the database. The database was prepared outside the application.
		if (sqlite3_open([[SQLite fullFilePath] UTF8String], &database) == SQLITE_OK) {
			// Get the primary key for all books.
			const char *sql = "SELECT ID, Name, ShoppingImage, SortOrder FROM CategoryItem WHERE ID =?";
			sqlite3_stmt *statement;
			// Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
			// The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.
			if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
				// We "step" through the results - once for each row.
				sqlite3_bind_int(statement, 1, itemID);

				while (sqlite3_step(statement) == SQLITE_ROW) {
					// The second parameter indicates the column index into the result set.
                    self.ID = itemID;
                    self.Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
                    self.ShoppingImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
                    self.SortOrder = sqlite3_column_int(statement, 3);
			// "Finalize" the statement - releases the resources associated with the statement.
		} else {
			// Even though the open failed, call close to properly clean up resources.
			NSLog(@"Failed to open database with message '%s'.", sqlite3_errmsg(database));
			// Additional error handling, as appropriate...

	return self;


    NSMutableArray *listArray = [[[NSMutableArray alloc] init] autorelease];

    FMDatabase* db = [FMDatabase databaseWithPath:[SQLite fullFilePath]];
    if (![db open]) {
        NSLog(@"Could not open db.");

    FMResultSet *s = [db executeQuery:@"SELECT ID, Name, ShoppingImage, SortOrder FROM CategoryItem ORDER BY SortOrder"];

    while ([s next]) {

                CategoryItem *categoryItem = [[CategoryItem alloc] init];
                categoryItem.ID = [s intForColumnIndex:0];
                categoryItem.Name = [s stringForColumnIndex:1];
                categoryItem.ShoppingImage = [s stringForColumnIndex:2];
                categoryItem.SortOrder = [s intForColumnIndex:3];

                [listArray addObject:categoryItem];

                [categoryItem release];
    return listArray;

- (void)dealloc {
	[super dealloc];

    [Name release];
    [ShoppingImage release];