0
我工作的一個iPhone應用程序,我已經定義的類像這樣:NSMutableArray的XCode中
@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的。我已經在主代碼中定義了NSMutableArray(稱爲AllTheProbes),然後此代碼嘗試查找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
我在這個數組上的@NorthCode沒有被初始化。 – 2011-05-22 23:32:19
這是問題所在。謝謝。 – NCGrimbo 2011-05-23 23:16:49