XCode中的类的NSMutableArray

时间:2011-05-21 14:20:28

标签: xcode class nsmutablearray

我正在研究iPhone应用程序,我已经定义了一个类:

@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

在我的主代码中,我需要创建此类的NSMutableArray。我已经在主代码(称为AllTheProbes)中定义了NSMutableArray,然后此代码尝试查找sProbeID,如果找不到它,它会向数组中添加一个新的PlotData类。

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

    }
    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.");
}

我遇到的问题是数据似乎没有存储。如果未找到探测器,则在将新的PlotData添加到AllTheProbes阵列后,程序仍然找不到该记录。这是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.

请注意,第3个输出行显示它添加的记录为-1,这意味着在添加后没有找到它。

谁能告诉我我做错了什么?

谢谢, NCGrimbo

1 个答案:

答案 0 :(得分:1)

您是否已分配数据?

像这样:

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

你为什么要打电话给自己.AllTheProbes?不应该只是“AllTheProbes”吗?

希望有所帮助。