我编写了一个测试代码来理解 Objective C 与 C 指针。这是代码..
例如:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableArray *sec_mut;
if (array) {
[array addObject:@"My"];
[array addObject:@"name"];
[array addObject:@"is"];
[array addObject:@"BoB"];
sec_mut = [[NSMutableArray alloc] init];
if (sec_mut) {
for (id obj in array) { // Is it allocating new object and adding or only points to already existing memory (just adding address)?
[sec_mut addObject:obj];
}
}
}
[array removeObjectAtIndex:0];
for (id obj in sec_mut) {
NSLog(@"str is %@\n",obj);
}
输出: str is My str is name str is is str is BoB
即使我已经删除了索引为零的对象,所有 4 个对象都在新数组中正确显示,所以它不仅指向并且它有自己的对象,对吗?还是它是一个悬空指针?
谢谢