OC是C的超集,指针的意义和使用方法自然是一脉相承的。首先需要明确的是:对于函数,所有参数的传递,都是传递值的拷贝(这里涉及到函数参数调用时的压栈出栈操作)。
既然所有的参数传递都是传递值的拷贝,那么指针也不例外。传递进去的同样也是指针的拷贝。如下代码:
#include <stdio.h>
void increase(int * x){
printf("xb:%p,%p--%d\n",&x,x,*x);
int b = 2;
x = &b;
printf("xa:%p,%p--%d\n",&x,x,*x);
}
int main(int argc, const char * argv[]) {
// insert code here...
int i,b;
int *j = &b;
i = 10;
b = 20;
printf("ib:%p--%d\n",&i,i);
printf("jb:%p,%p--%d\n",&j,j,*j);
increase(&i);
increase(j);
printf("ia:%p--%d\n",&i,i);
printf("ja:%p,%p--%d\n",&j,j,*j);
return 0;
}
运行结果如下:
ib:0x7ffeefbff5ac--10
jb:0x7ffeefbff5a0,0x7ffeefbff5a8--20
xb:0x7ffeefbff578,0x7ffeefbff5ac--10
xa:0x7ffeefbff578,0x7ffeefbff574--2
xb:0x7ffeefbff578,0x7ffeefbff5a8--20
xa:0x7ffeefbff578,0x7ffeefbff574--2
ia:0x7ffeefbff5ac--10
ja:0x7ffeefbff5a0,0x7ffeefbff5a8--20
可以看到:对于指针变量j,实参存储空间为0x7ffeefbff5a0,形参存储空间为0x7ffeefbff578,发生了指针的拷贝,但此时两个指针指向是同一块内存0x7ffeefbff5a8。当在函数内部操作指针自身,只会改变形参指针的指向,而不会改变实参的数据。对于int变量i也一样,只是在把i本身地址当作指针,函数内同样开辟一块内存0x7ffeefbff578来指向这个所谓的指针(i的地址)。
那么在OC中:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *outerStr = @"outer";
NSLog(@"outer before:%p,%p----%@",&outerStr,outerStr,outerStr);
[self changeStr:outerStr];
NSLog(@"outer after:%p,%p----%@",&outerStr,outerStr,outerStr);
}
- (void)changeStr:(NSString *)innerStr{
NSLog(@"inner before:%p,%p----%@",&innerStr,innerStr,innerStr);
NSString *str = @"changeInner";
innerStr = str;
NSLog(@"inner str after:%p,%p----%@",&str,str,str);
NSLog(@"inner after:%p,%p----%@",&innerStr,innerStr,innerStr);
}
2019-01-01 15:50:24.528321+0800 parameterTest[94341:6658077] outer before:0x7ffee6bd4a28,0x10902a068----outer
2019-01-01 15:50:24.528481+0800 parameterTest[94341:6658077] inner before:0x7ffee6bd49e8,0x10902a068----outer
2019-01-01 15:50:24.528586+0800 parameterTest[94341:6658077] inner str after:0x7ffee6bd49e0,0x10902a0e8----changeInner
2019-01-01 15:50:24.528710+0800 parameterTest[94341:6658077] inner after:0x7ffee6bd49e8,0x10902a0e8----changeInner
2019-01-01 15:50:24.528804+0800 parameterTest[94341:6658077] outer after:0x7ffee6bd4a28,0x10902a068----outer
看的更清楚了,函数内只是改变了指针的指向。对于OC对象,我们不能直接操作指针所指的实际的内容,那么只能传进来双层指针,改变指针的指针了。例如NSError **。
参考:说说OC参数传递的那些坑