NSMutableArray of Classes in XCode

I'm working on an iPhone app and I have defined a class as so:

@interface PlotData : NSObject {
    NSString *sProbeID;
    NSMutableArray *dataPoints;

@property (nonatomic, retain) NSString *sProbeID;
@property (nonatomic, retain) NSMutableArray *dataPoints;


@implementation PlotData

@synthesize sProbeID;
@synthesize dataPoints;

- (void)dealloc {
    [sProbeID release];
    [dataPoints release];

    [super dealloc];


In my main code, I need to create a NSMutableArray of this class. I've got the NSMutableArray defined in the main code (called AllTheProbes) and then this code attempts to find the sProbeID and if it doesn't find it, it adds a new PlotData class to the array.

-(void) AddDataPointDictionary:(NSDictionary *)aDict WithProbe:(ProbeObj *)aProbe{

    NSLog(@"In AddDataPointDictionary.");

    //The first step is to find the probe.
    int nProbeLoc = -1;
    PlotData *aPlotDataObj;

    for (int i=0; i < [self.AllTheProbes count]; i++) {
        aPlotDataObj = [self.AllTheProbes objectAtIndex:i];
        if (aPlotDataObj.sProbeID == aProbe.sID) {
            nProbeLoc = i;
    if (nProbeLoc == -1) {
        NSLog(@"  Did not find the record for %@.", aProbe.sID);
        //We need to add this probe to the array of all probes.
        PlotData *newPlot = [[PlotData alloc]init];
        newPlot.sProbeID = aProbe.sID;
        NSMutableArray *newArr = [[NSMutableArray alloc]initWithCapacity:0];
        newPlot.dataPoints = newArr;
        [self.AllTheProbes addObject:newPlot];
        [newPlot release];
        [newArr release];

        //set aPlotDataObj equal to the object we just added.
        for (int i=0; i < [self.AllTheProbes count]; i++) {
            aPlotDataObj = [self.AllTheProbes objectAtIndex:i];
            if (aPlotDataObj.sProbeID == aProbe.sID) {
                nProbeLoc = i;
        NSLog(@"  Found the added record at %d.", nProbeLoc);
        aPlotDataObj = [self.AllTheProbes objectAtIndex:nProbeLoc];

        NSLog(@"  Found %@.", aPlotDataObj.sProbeID);
        //Use the record we found
        aPlotDataObj = [self.AllTheProbes objectAtIndex:nProbeLoc];

    //Add the dictionary to the plot array
    [aPlotDataObj.dataPoints addObject:aDict];
    NSLog(@"   Point added.");

The problem I am having is that the data does not appear to get stored. When a probe is not found, after adding the new PlotData to the AllTheProbes array, the program still does not find the record. Here's the output from the NSLogs.

2011-05-21 09:53:24.600 Stoker Monitor[4545:207] In AddDataPointDictionary.
2011-05-21 09:53:24.601 Stoker Monitor[4545:207]   Did not find the record for 7200001259348330.
2011-05-21 09:53:24.601 Stoker Monitor[4545:207]   Found the added record at -1.
2011-05-21 09:53:24.602 Stoker Monitor[4545:207]    Point added.

Notice that the 3rd output line says it found the added record at -1, which means it did not find it after adding it.

Can anyone tell me what I am doing wrong?

Thanks, NCGrimbo


Have you alloc, inited the array?

Like so:

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

And why are you calling self.AllTheProbes? shouldnt it just be "AllTheProbes"?

Hope that helps.

Need Your Help

Music API that has song release date/year

api music last.fm gracenote

I'm looking for a music API that can look up info of a song provided artist and track name, and returns the year that the song was released.

XCode cannot find foundation.h or UIKit.h after MediaPlayer.framework is added

iphone objective-c xcode media-player ios-frameworks

As soon as I add the MediaPlayer.framework into a new program, XCode absolutely shits its pants. Under supporting Files, PROJECTNAME-Prefix.phc starts red-flagging both #import and #import

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.