XCode 中类的 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。我在主代码中定义了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你分配、初始化了数组吗?
就像这样:
为什么你要调用 self.AllTheProbes?不应该只是“AllTheProbes”吗?
希望有帮助。
Have you alloc, inited the array?
Like so:
And why are you calling self.AllTheProbes? shouldnt it just be "AllTheProbes"?
Hope that helps.