开发者

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;

@end

@implementation PlotData

@synthesize sProbeID;
@synthesize dataPoints;



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

    [super dealloc];
}

@end

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];开发者_JAVA技巧
        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];

    }
    else{
        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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜