0

SO here's my setup. I have an object called radiostations where I have several strings like callsign, frequency declared and an NSMutableArray called amStationInfo. On my viewcontroller, I access an SQLite database which populates the an array like so...

radiostations.h

@interface radiostations : NSObject {

    NSString *format;
    NSString *city;
}

@property (nonatomic, retain) NSString *format;
@property (nonatomic, retain) NSString *city;

ViewController.m

radiostations *amStationClass = [[radiostations alloc] init];
NSMutableArray* amStationInfo = [[NSMutableArray alloc] init];

while (sqlite3_step(statement) == SQLITE_ROW)
    {
    NSString *cityField = [[NSString alloc] initWithUTF8String:
                                   (const char *) sqlite3_column_text(statement, 10)];
    NSString *formatField = [[NSString alloc] initWithUTF8String:
                                    (const char *) sqlite3_column_text(statement, 0)];
    [amStationInfo addObject:amStationClass];
                    [amStationClass setCity:cityField];
                    [amStationClass setFormat:formatField];
    }
[tabView reloadData];
sqlite3_finalize(statement);

and then I populate a UITableView

NSString *cityValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] city];
NSString *formatValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] format];
cityLabel.text = cityValue;
formatLabel.text = formatValue;

Initially I was dealing with a few Arrays and this worked just fine. I then changed it so that I was only dealing with one array using a class object and now it's not working. I know the SQLite query and what not works so Im not having any problems with that. It seems as though the array does not get populated.

3
  • Your naming is a mess; radiostations should be RadioStations, amStationClass should be amStation (it isn't a class, it is an instance), amStationInfo should probably be amStationsArray, cityField should be city or cityName, etc... also, if you aren't using ARC, you'll be leaking two strings, too. Commented Apr 24, 2012 at 19:29
  • Thanks for the tip, I am quite new and I just realized that. I am using ARC however. Commented Apr 24, 2012 at 19:34
  • No worries; Objective-C, iOS, Cocoa, and all the frameworks in between are very much built on a whole slew of common patterns. If you learn those patterns and follow them, it'll make things a whole lot simpler! Commented Apr 24, 2012 at 19:56

2 Answers 2

2

You are changing the properties of the same radiostations object and adding it over and over again to the array. You need to create a new radiostations object for each row from your sqlite database and add this:

while (...) {
    // fetch data as before

    radiostations *record = [[radiostations alloc] init];
    [record setCity: cityField];
    [record setFormat: formatField];
    [amStationInfo addObject: record];
    [record release];
}

If you are using ARC you need to remove the line [record release];, otherwise it is necessary to avoid leaking those objects.

Sign up to request clarification or add additional context in comments.

2 Comments

I am using ARC and therefore I am not releasing amStationClass
It works now, it turns out, I needed to allocate *record inside the while statement and not outside and I added the object into the Array after i set the strings... Thanks!
1

where did you allocate/init your mutablearray? something like:

NSMutableArray* amStationInfo = [[NSMutableArray alloc] init];

you need to allocate it once, before to add objects in it

3 Comments

I forgot to allocate it, and even though I did, it still does not populate
i confess i read your question in hurry, when i noticed that error, but probably other are in... what about Sven answer?
Sven's answer worked. I changed two things, I allocated the instance inside the while loop and added in into the array after I set the strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.