2011-12-18 188 views
1

我有以下屬性和方法的類:下面迭代通過對象的一個​​NSMutableArray

頭文件 - 注意,我並沒有複製/粘貼的所有代碼(僅pertinant信息):

@interface SQLiteDB : NSObject 

@property (nonatomic, strong) NSMutableArray *allAccountsArray; 
@property (nonatomic, strong) NSString *accountId, *accountName, *accountDescription, *accountTags, *accountPhoto, *accountCreationDate; 

+(id) populateAccountObjectWithId:(NSString *)id andName:(NSString *)name andDescription:(NSString *)description andTags:(NSString *)tags andPhoto:(NSString *)photo andCreationDate:(NSString *)creationDate; 

@end 

下面執行文件 - 注意,我並沒有複製/粘貼的所有代碼(僅pertinant信息):

+(id) populateAccountObjectWithId:(NSString *)id andName:(NSString *)name andDescription:(NSString *)description andTags:(NSString *)tags andPhoto:(NSString *)photo andCreationDate:(NSString *)creationDate 
{ 
    SQLiteDB *mySQLiteDB = [[self alloc] init]; 
    mySQLiteDB.accountId = id; 
    mySQLiteDB.accountName = name; 
    mySQLiteDB.accountDescription = description; 
    mySQLiteDB.accountTags = tags; 
    mySQLiteDB.accountPhoto = photo; 
    mySQLiteDB.accountCreationDate = creationDate; 
    return mySQLiteDB; 
} 

然後,在實現文件的另一種方法獲取來自SQLite的DAT所有帳戶ABASE:

-(id) fetchAccountList 
{ 
    // do some database stuff here 
    // create prepared statement, open database, etc... 

    allAccountsArray = [[NSMutableArray alloc] init]; 

    while(sqlite3_step(statement) == SQLITE_ROW) 
    { 
     NSString *thisAccountId =    [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)]; 
     NSString *thisAccountName =   [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 
     NSString *thisAccountDescription =  [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]; 
     NSString *thisAccountTags =   [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]; 
     NSString *thisAccountPhoto =   [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)]; 
     NSString *thisAccountCreationDate = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)]; 

     [allAccountsArray addObject:[SQLiteDB populateAccountObjectWithId:thisAccountId andName:thisAccountName andDescription:thisAccountDescription andTags:thisAccountTags andPhoto:thisAccountPhoto andCreationDate:thisAccountCreationDate]]; 

    } 
    // error handling code, etc. 
    // finalize, & close code here... 

return allAccountsArray; 

}

現在總算問題。在其他類中,我想用這個返回的對象數組來做東西。比如我會在TableVeiw控制器做到這一點:

-(void)loadView 
{ 
    [super loadView]; 

    mySQLiteDB = [[SQLiteDB alloc] init]; 
    allAccountsArray = [mySQLiteDB fetchAccountList]; 
} 

我會用這個晚些時候例如填充cellForRowAtIndexPath方法中的表列表。也許表格的每個單元格都會包含accountName,accountDescription和accountCreationDate。我不知道但是如何從對象的數組中訪問該名稱,降序,日期...

這顯然會產生一個錯誤:

cell.textLabel.text = [allAccountsArray objectAtIndex:indexPath.row]; 

因爲「行」的對象是「對象「包含名稱,desc,日期等...

所以Stackoverflow,我問你...如何完成獲取對象變量在數組的每個元素?

回答

2

你應該能夠做到這樣簡單的事情,因爲這:

SqliteDB *mySqliteDB = (SQliteDB *)[allAccountsArray objectAtIndex:indexPath.row]; 
NSString *myText = mySqliteDB.thisAccountID; 
myText = [myText stringByAppendingString:mySqliteDB.thisAccountName]; 
.... etc. 
cell.textLabel.text = myText; 
+0

這工作完美,非常感謝你! – ElasticThoughts

1

我覺得enumerateObjects:usingBlock:是你想要的迭代,即枚舉,對象。你可能錯過了它,因爲它在超類中。

+0

嗡嗡聲,好的我從來沒有用過enumerateObjects:usingBlock:但是會查看它!感謝發佈!僅供參考 - 我需要約4周的時間來開發任何使用Obj C的東西。 – ElasticThoughts