У меня есть класс со следующим свойством и методом:Итерация через 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 ДАТ унизит:
-(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. Возможно, каждая ячейка таблицы будет содержать имя учетной записи, accountDescription и accountCreationDate. Я, однако, не знаю, как получить доступ к этому имени, DESC, дата внутри массива объектов ...
Это, очевидно, приводит к ошибке:
cell.textLabel.text = [allAccountsArray objectAtIndex:indexPath.row];
, так как объект в «строке» является " объект ", содержащий имя, desc, date и т. д.
Итак, Stackoverflow, я спрашиваю вас ... Как достичь получения переменных объекта у каждого элемента массива?
Это работает отлично, спасибо вам большое! – ElasticThoughts