程序收到信号“EXC_Bad_Access”NSMutableArray

时间:2011-05-02 19:15:04

标签: iphone objective-c arrays ios

如果我尝试重新运行下面的代码,我会在[categoryList count]上收到一条EXE_bad_access消息

NSMutableArray *categoryList = [[CategoryItem alloc] getAll];
NSLog(@"number of items is %@", [categoryList count]);

课程在

之下
#import "CategoryItem.h"

#import "SQLite.h"

@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 *)getAll;

@end
@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.
            sqlite3_finalize(statement);
        } else {
            // Even though the open failed, call close to properly clean up resources.
            sqlite3_close(database);
            NSLog(@"Failed to open database with message '%s'.", sqlite3_errmsg(database));
            // Additional error handling, as appropriate...
        }

    }
    return self;
}

-(NSMutableArray*)getAll{

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

    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 ORDER BY SortOrder";
        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.
            while (sqlite3_step(statement) == SQLITE_ROW)
            {
                // The second parameter indicates the column index into the result set.

                CategoryItem *categoryItem = [[CategoryItem alloc] init];

                categoryItem.ID = sqlite3_column_int(statement, 0);
                categoryItem.Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
                categoryItem.ShoppingImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
                categoryItem.SortOrder = sqlite3_column_int(statement, 3);

                [listArray addObject:categoryItem];

                [categoryItem release];
                categoryItem = nil;

            }


        }else{
            printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) ); 
        }
        // "Finalize" the statement - releases the resources associated with the statement.
        sqlite3_finalize(statement);

    } else {
        // Even though the open failed, call close to properly clean up resources.
        sqlite3_close(database);
        NSLog(@"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        // Additional error handling, as appropriate...
    }

    //NSLog(@"this is the list array count %@", [listArray count]);

    return listArray;
}



- (void)dealloc {
    [super dealloc];
    [Name release];
    [ShoppingImage release];

}


@end

2 个答案:

答案 0 :(得分:2)

您创建CategoryItem的方式似乎不正确。您正在调用alloc,但不是init...方法。您可能希望使用在实现中提供的initWithObject方法。

来自Apple docs

  

创建对象需要两个步骤   使用Objective-C。你必须:

     
      
  • 动态分配内存   新对象

  •   
  • 初始化   将内存分配给适当的值

  •   
     

直到对象才完全正常工作   这两个步骤都已完成。每   步骤是通过单独完成的   方法,但通常在一行   代码:

     

id anObject = [[Rectangle alloc]   INIT];

修改

除了初始化问题之外,似乎存在一个概念问题(由@Terry Wilcox指出): 在实例上调用方法getAll似乎没有意义,因此应该将其定义为类方法:

+ (NSMutableArray*)getAll;

应该像这样调用:

NSMutableArray *categoryList = [CategoryItem getAll];

编辑2:

您的日志声明似乎也不正确。 [categoryList count]返回NSUInteger,您尝试使用%@打印对象。请改用%i

NSLog(@"number of items is %i", [categoryList count]);

答案 1 :(得分:0)

此代码:

NSMutableArray *categoryList = [[CategoryItem alloc] getAll];

没有意义。如果getAll是CategoryItem上的类方法,那么它应该被定义为

+ (NSMutableArray*)getAll;

你应该把它称为

NSMutableArray *categoryList = [CategoryItem getAll];

然后categoryList将是您不拥有的数组,因此您可能希望在获得它时保留它。