2011-05-02 48 views
0

如果我嘗試和reun下面的代碼,我得到的[所屬分類計數]的EXE_bad_access消息程序接收到的信號「EXC_BAD_ACCESS」的NSMutableArray

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 
+3

只有受虐者直接在Objective-C中使用SQLite C API。 [使用FMDB](http://github.com/ccgus/fmdb)(一個SQLite包裝器)或[CoreData](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/ cdProgrammingGuide.html)(一個對象圖管理器)。 – 2011-05-02 19:17:38

+0

@Luke不應該初始化那個'CategoryItem'?你正在調用'alloc'而不是'initWithObject' ... – albertamg 2011-05-02 19:25:40

+0

你還沒有初始化類CategoryList,但你調用它的方法。似乎問題出現在(不存在的)初始化中。 – 2011-05-02 19:27:51

回答

2

它不看起來就像你創建你的CategoryItem一樣。您正在致電alloc,但沒有任何init...方法。您可能想要使用您在實施中提供的initWithObject方法。

Apple docs

需要兩個步驟來創建使用Objective-C的對象 。你必須:

  • 動態分配內存 新對象

  • 初始化新 分配的內存爲適當的值

的對象是沒有充分發揮作用,直到 兩個步驟已經完成。每個 步驟由一個獨立的 方法,但通常在單個行代碼 完成:

ID anObject = [[矩形的alloc] INIT];

編輯:

除了初始化的問題,似乎有一個概念性的問題(由@Terry威爾科克斯指出): 調用方法getAll上的一個實例似乎沒有什麼意義和因此應該被定義爲一類方法來代替:

+ (NSMutableArray*)getAll; 

和應該被稱爲像這樣:

NSMutableArray *categoryList = [CategoryItem getAll]; 

編輯2:

你的日誌語句看起來不正確或者。 [categoryList count]返回NSUInteger,並且您試圖使用%@打印對象。使用%i代替:

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

代碼沒有試圖創建一個CategoryItem,它試圖創建一個NSMutableArray。 – 2011-05-02 20:02:43

+0

是的,如上我已經在調用alloc init? – Luke 2011-05-03 00:32:32

+0

@Luke你也試圖記錄一個int,就好像它是一個對象(見編輯2) – albertamg 2011-05-03 13:56:32

0

此代碼:

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

沒有意義。如果GETALL是CategoryItem一個類的方法,那麼它應該被定義爲

+ (NSMutableArray*)getAll; 

,你應該把它作爲

NSMutableArray *categoryList = [CategoryItem getAll]; 

然後所屬分類將是你沒有自己的數組,所以你可能想要保留它,當你得到它。

+0

感謝那些更乾淨的代碼!仍然不能解決我的記憶問題,雖然 – Luke 2011-05-03 00:37:43

+0

它不是更乾淨的代碼,它是工作代碼。您的原始代碼無法正常工作,它不會調用init,因此您的getAll方法可能不會返回任何內容。 – 2011-05-03 00:50:52

相關問題