gpt4 book ai didi

objective-c - 遍历对象的 NSMutableArray

转载 作者:行者123 更新时间:2023-12-03 16:58:55 25 4
gpt4 key购买 nike

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

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

@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 Controller 中执行此操作:

-(void)loadView
{
[super loadView];

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

稍后我将使用它来填充 cellForRowAtIndexPath 方法中的表列表。也许表的每个单元格都包含 accountName、accountDescription 和 accountCreationDate。然而,我不知道如何从对象数组中访问该名称、描述、日期...

这显然会产生错误:

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

因为“row”处的对象是一个包含名称、desc、日期等的“对象”...

那么 Stackoverflow,我问你...如何实现获取数组每个元素的对象变量?

最佳答案

您应该能够做这样简单的事情:

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

关于objective-c - 遍历对象的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8552704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com