迭代对象的NSMutableArray

时间:2011-12-18 15:25:27

标签: objective-c cocoa-touch cocoa ios5

我有一个具有以下属性和方法的类:

下面的头文件 - 注意我没有复制/粘贴所有代码(仅限于相关信息):

@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

下面的实施文件 - 注意我没有复制/粘贴所有代码(仅限于相关信息):

+(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方法中的表列表。也许表的每个单元格都包含accountName,accountDescription和accountCreationDate。但是,我不知道如何从对象数组中访问该名称,desc,date ...

这显然会产生错误:

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

因为“row”处的对象是一个包含name,desc,date等的“对象”......

所以Stackoverflow,我问你......我如何在数组的每个元素上获得对象变量?

2 个答案:

答案 0 :(得分:2)

你应该可以做一些简单的事情:

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

答案 1 :(得分:1)

我认为enumerateObjects:usingBlock:是你想要迭代的东西,即枚举对象。你可能错过了它,因为它在超类中。