XCode 中类的 NSMutableArray

发布于 2024-11-09 09:27:26 字数 2761 浏览 0 评论 0原文

我正在开发一个 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.

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

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

谢谢, 北卡罗来纳州林博

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2024-11-16 09:27:26

你分配、初始化了数组吗?

就像这样:

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

为什么你要调用 self.AllTheProbes?不应该只是“AllTheProbes”吗?

希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文