我有以下屬性和方法的類:下面迭代通過對象的一個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,我問你...如何完成獲取對象變量在數組的每個元素?
這工作完美,非常感謝你! – ElasticThoughts