1

I have a class with the following property and method:

header file below - note I did not copy/paste all code (only pertinant information):

@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

implementation file below - note I did not copy/paste all code (only pertinant information):

+(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;
}

Then, another method in the implementation file fetches all accounts from the SQLite database:

-(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;

}

Now finally the question. In other classes I want to do stuff with the array of objects that this returns. For instance I would do this in a TableVeiw controller:

-(void)loadView
{
    [super loadView];

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

I would use this later to for instance populate the table list in the cellForRowAtIndexPath method. Perhaps each cell of the table would contain the accountName, accountDescription, and accountCreationDate. I do not however know how to access that name, desc, date from within the array of objects...

This obviously produces an error:

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

because the object at "row" is an "object" containing name, desc, date, etc...

So Stackoverflow, I ask you... How do I accomplish getting the object variables at each element of the array?

2 Answers 2

2

You should be able to do something as simple as this:

SqliteDB *mySqliteDB = (SQliteDB *)[allAccountsArray objectAtIndex:indexPath.row];
NSString *myText = mySqliteDB.thisAccountID;
myText = [myText stringByAppendingString:mySqliteDB.thisAccountName];
.... etc.
cell.textLabel.text = myText;
Sign up to request clarification or add additional context in comments.

Comments

1

I think enumerateObjects:usingBlock: is what you want for iterating, i.e. enumerating, objects. You might have missed it because it's in the superclass.

1 Comment

Hummm, ok I've never used enumerateObjects:usingBlock: however will look into it! Thanks for posting! FYI -- I'm about 4 weeks into ever developing anything using Obj C.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.