【问题标题】:Iterate through an NSMutableArray of Objects遍历对象的 NSMutableArray
【发布时间】:2011-12-18 15:25:27
【问题描述】:

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

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

@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。但是我不知道如何从对象数组中访问该名称、描述、日期...

这显然会产生错误:

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

因为“行”处的对象是包含名称、描述、日期等的“对象”...

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

【问题讨论】:

    标签: objective-c cocoa-touch cocoa ios5


    【解决方案1】:

    你应该可以做这么简单的事情:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 嗯,好吧,我从来没有使用过 enumerateObjects:usingBlock: 但是会调查一下!感谢您的发布!仅供参考——我使用 Obj C 开发任何东西大约需要 4 周时间。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2014-08-09
      • 2018-07-15
      相关资源
      最近更新 更多