Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 更改变量';s值通过另一个变量传递_Objective C_Variables_Pointers_Variable Assignment - Fatal编程技术网

Objective c 更改变量';s值通过另一个变量传递

Objective c 更改变量';s值通过另一个变量传递,objective-c,variables,pointers,variable-assignment,Objective C,Variables,Pointers,Variable Assignment,如何用一个变量a替换另一个变量b,以更改b 例如: NSString *a = @"a"; NSString *b = @"b"; NSString *c = @"c"; a = b; a = c; 在这种情况下,b的值是@“b”,对吗?我想在不使用b=c的情况下,生成b@“c”的值。也许我应该试着理解指针 请理解我拙劣的解释,并尽可能给我任何建议。你可能会感到困惑,因为坦率地说,指针一开始有点困惑。它们是保存内存位置的变量。如果有两个指针保持相同的位置,并且使用一个指针更改该位置的内容,则

如何用一个变量
a
替换另一个变量
b
,以更改
b

例如:

NSString *a = @"a";
NSString *b = @"b";
NSString *c = @"c";

a = b;
a = c;
在这种情况下,
b
的值是
@“b”
,对吗?我想在不使用
b=c
的情况下,生成
b
@“c”
的值。也许我应该试着理解指针


请理解我拙劣的解释,并尽可能给我任何建议。

你可能会感到困惑,因为坦率地说,指针一开始有点困惑。它们是保存内存位置的变量。如果有两个指针保持相同的位置,并且使用一个指针更改该位置的内容,则可以通过另一个指针查看这些新内容。不过,它仍然指向同一个位置

int x = 10;

// xp is a pointer that has the address of x, whose contents are 10
int * xp = &x;
// yp is a pointer which holds the same address as xp
int * yp = xp;

// *yp, or "contents of the memory address yp holds", is 10
NSLog(@"%i", *yp);

// contents of the memory at x are now 62
x = 62;

// *yp, or "contents of the memory address yp holds", is now 62
NSLog(@"%i", *yp);
// But the address that yp holds has _not_ changed.
根据您的评论,是的,您可以这样做:

int x = 10;
int y = 62; 

// Put the address of x into a pointer
int * xp = &x;
// Change the value stored at that address
*xp = y;

// Value of x is 62
NSLog(@"%i", x);
您可以使用
NSString
s做同样的事情,尽管我想不出这样做的好理由。将示例中的任何
int
更改为
NSString*
int*
变为
NSString**
。根据需要更改分配和
NSLog()
格式说明符:

NSString * b = @"b";
NSString * c = @"c";

// Put the address of b into a pointer
NSString ** pb = &b;
// Change the value stored at that address
*pb = c;
// N.B. that this creates a memory leak unless the previous
// value at b is properly released.

// Value at b is @"c"
NSLog(@"%@", b);

你可能会感到困惑,因为坦率地说,指针一开始有点困惑。它们是保存内存位置的变量。如果有两个指针保持相同的位置,并且使用一个指针更改该位置的内容,则可以通过另一个指针查看这些新内容。不过,它仍然指向同一个位置

int x = 10;

// xp is a pointer that has the address of x, whose contents are 10
int * xp = &x;
// yp is a pointer which holds the same address as xp
int * yp = xp;

// *yp, or "contents of the memory address yp holds", is 10
NSLog(@"%i", *yp);

// contents of the memory at x are now 62
x = 62;

// *yp, or "contents of the memory address yp holds", is now 62
NSLog(@"%i", *yp);
// But the address that yp holds has _not_ changed.
根据您的评论,是的,您可以这样做:

int x = 10;
int y = 62; 

// Put the address of x into a pointer
int * xp = &x;
// Change the value stored at that address
*xp = y;

// Value of x is 62
NSLog(@"%i", x);
您可以使用
NSString
s做同样的事情,尽管我想不出这样做的好理由。将示例中的任何
int
更改为
NSString*
int*
变为
NSString**
。根据需要更改分配和
NSLog()
格式说明符:

NSString * b = @"b";
NSString * c = @"c";

// Put the address of b into a pointer
NSString ** pb = &b;
// Change the value stored at that address
*pb = c;
// N.B. that this creates a memory leak unless the previous
// value at b is properly released.

// Value at b is @"c"
NSLog(@"%@", b);

除了写
b=c之外,我唯一能给你的建议是是学习如何以有意义的方式解释自己。无论我多么想要它,我都不可能理解它。除了写
b=c是学习如何以有意义的方式解释自己。无论我多么想要它,我都不可能理解它。再次感谢您的回答!是的,这正是我想做的。但我不能像这样声明“NSString**a;”。所以我要提出下一个问题来关注这个问题。谢谢我在这里问过,有时间请告诉我任何建议。谢谢再次回答!是的,这正是我想做的。但我不能像这样声明“NSString**a;”。所以我要提出下一个问题来关注这个问题。谢谢我在这里问过,如果你有时间,请告诉我任何建议。