NSMutableArray Классов в XCode - PullRequest
0 голосов
/ 21 мая 2011

Я работаю над приложением для 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 этого класса.У меня есть 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

1 Ответ

1 голос
/ 23 мая 2011

Вы выделили, инициализировали массив?

Вроде так:

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

А почему вы называете себя. AllTheProbes? это не должно быть "AllTheProbes"?

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...