好吧,我正在处理两组非常相似的数据,同时这些数据集都是对象内的全局NSMutableArrays。使用指针来调整目标中的全局对象-c
data_set_one = [[NSMutableArray alloc] init];
data_set_two = [[NSMutableArray alloc] init];
加载了两个新的NSMutableArrays,它们需要添加到旧的现有数据中。这些阵列也是全球性的。
xml_dataset_one = [[NSMutableArray alloc] init];
xml_dataset_two = [[NSMutableArray alloc] init];
为了减少重复代码(因为这些数据集是所以类似)我写的类内的空隙的方法来处理这两个阵列中的数据组合处理:
-(void)constructData:(NSMutableArray *)data fromDownloadArray:(NSMutableArray *)down withMatchSelector:(NSString *)sel_str
现在,我有一个体面的面向对象编程的理解,所以我在想,如果我要调用数据中的全局数组的方法如此... ...
[self constructData:data_set_one fromDownloadArray:xml_dataset_one withMatchSelector:@"id"];
然后,全局NSMutableArrays(data_set_one)将反映方法内发生的“数组”变化。可悲的是,情况并非如此,data_set_one并未反映方法外部的更改(例如:Array内的新对象)。
这里的问题是
// data_set_one is empty
// xml_dataset_one has a few objects
[constructData:(NSMutableArray *)data_set_one fromDownloadArray:(NSMutableArray *)xml_dataset_one withMatchSelector:(NSString *)@"id"];
// data_set_one should now be xml_dataset_one, but when echoed to screen, it appears to remain empty
的代码段,这里是该方法的代码的要点,任何帮助表示赞赏。
-(void)constructData:(NSMutableArray *)data fromDownloadArray:(NSMutableArray *)down withMatchSelector:(NSString *)sel_str {
if ([data count] == 0) {
data = down; // set data equal to downloaded data
} else if ([down count] == 0) {
// download yields no results, do nothing
} else {
// combine the two arrays here
}
}
该项目未启用ARC。
感谢您的帮助! Rob
你想做什么? –
我希望data_set_one保留从void方法给出的值。它被传入,然后通过指针“数据”进行更改。问题是,对数据所做的更改不是针对data_set_one进行的。编辑,对不起,我发现很难解释我的问题:/ – Rob
你需要保留它的价值。 newValue = oldValue,其中oldValue得到释放结果nil到newValue。这是这里的问题。 –