使用Objective C的简单复制指针 [英] Simple copying pointers with Objective C

查看:75
本文介绍了使用Objective C的简单复制指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用另一个指针来修改一个指针的内容.我希望string2指向string1指向的内容,以便当我修改string2时,它会修改string1.我该怎么办?

I'm trying to modify the contents of one pointer using another. I want string2 to point to what string1 points to, so that when I modify string2, it modifies string1. How can I do this?

 NSString *string1=@"hello";
 NSString *string2=string1;
 string2=@"changed";
 NSLog(@"string1:%@, string2:%@", string1, string2); //"string1:hello, string2:changed"

推荐答案

string2变量进行第二次赋值时,您不会修改变量当前所指的内容,在这种情况下,相同的作为变量string1所指,但是您正在修改string2所指.

When you make a second assignment to your string2 variable you are not modifying the contents of what the variable currently refers to, in this case the same NSString as variable string1 refers to, but rather your are modifying to what string2 refers.

一张图片(代码)值一千字:

A picture (code) is worth a thousand words:

每个对象和变量都有一个内部名称,通常称为其地址.地址通常用十六进制数字表示,例如0xdeadbeef,数字的对您并不重要-只是一个标签.

Every object and variable has an internal name, usually called its address. Addresses are usually written as hexadecimal numbers, e.g. 0xdeadbeef, the value of the number is not important to you - it is just a label.

撰写时:

NSString *string1 = @"Hello!";

右侧的

要求Objective-C创建一个值为@"Hello"NSString,其结果是一个地址,例如0xfeeddeaf. lhs端要求一个能够在其中保存NSString *的变量-对NSString的引用;将此变量称为string1;并将rhs 0xfeeddeaf的结果存储到其中.

the right-hand side asks Objective-C to create an NSString with the value @"Hello", the result of this is an address, say 0xfeeddeaf. The lhs-side asks for a variable capable of holding an NSString * in it - a reference to an NSString; to call this variable string1; and to store the result of the rhs, 0xfeeddeaf, into it.

现在编写:

NSString *string2 = string1;

这将导致变量string2也包含0xfeeddeaf. string1string2的内容之间没有链接,分配仅复制一个值(在这种情况下为地址).

this results in a variable string2 also containing 0xfeeddeaf. There is no linkage between the contents of string1 and string2, assignment just copies a value (which is an address in the case).

最后,当你写:

string2 = @"changed";

这将创建一个NSString,其值为@"changed",其地址为0xbeedcafe,并将其存储在string2中.因此,现在string1包含0xfeeddeaf,它是@Hello的地址,而string2包含0xbeedcafe,它是@"changed"的地址.

this creates an NSString with the value of @"changed" with an address, say, of 0xbeedcafe and stores that in string2. So now string1 contains 0xfeeddeaf which is the address of @Hello, and string2 contains 0xbeedcafe which is the address of @"changed".

我猜您写正在尝试使用另一个指针修改一个指针的内容"时,string1string2想要引用的是 same 对象,您可以修改其值.您不能修改NSString的值,因为它们一旦创建就不可修改(不可变).取而代之的是,您必须使用NSMutableString ,而您需要使用一种方法来更改NSMutableString对象的值-如上所示,如果您使用赋值,则可以更改所引用的对象,不是对象的内容.

I'm guessing when you write that you're "trying to modify the contents of one pointer using another" what you want is for string1 and string2 to refer to the same object, whose value you can modify. You can't modify the value of an NSString, they are non-modifiable (immutable) once created. Instead you have to use an NSMutableString and you need to use a method to change the value of an NSMutableString object - as the above shows if you use assignment you change which object is referred to, not the contents of the object.

假设您正在使用ARC [*],然后将代码更改为:

Assuming you are using ARC[*] then changing your code to:

NSMutableString *string1 = [NSMutableString stringWithString:@"Hello"];
NSMutableString *string2 = string1;
[string2 setString:@"changed"];

将实现我认为您想要的.前两行的执行方式与上述类似,最后得到的string1string2包含相同的地址.第三行更改了两者都指向的地址-您具有所需的链接.

will accomplish what I think you intend. The first two lines execute similarly to above and you end up with string1 and string2 containing the same address. The third line changes what is at the address they both refer to - you have the linkage you intended.

[*](如果使用的是GC或retain/release),则将第一行的rhs更改为[[NSMutableString alloc] initWithString:"@Hello"]

[*] if you are using GC or retain/release change the rhs of the first line to [[NSMutableString alloc] initWithString:"@Hello"]

这篇关于使用Objective C的简单复制指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆