简体   繁体   English

Objective-C指针和NSString

[英]Objective-c pointers and NSString

If I have this code, why doesn't the textview's text update? 如果我有此代码,为什么textview的文本不更新? As far as I knew a * meant a pointer, and I haven't done a copy. 据我所知,*表示指针,而我还没有复制。

NSString *searchText = myTextView.text;

searchText = [searchText stringByReplacingOccurrencesOfString:@" " withString:@";"];

So why isn't myTextView's text changed as if I did: 那么为什么myTextView的文本没有像我一样更改:

myTextView.text = [searchText stringByReplacingOccurrencesOfString:@" " withString:@";"];

And how would I write the code, so that the first code example works as I intend? 以及如何编写代码,以便第一个代码示例按预期工作?

The method stringByReplacing... Doesn't change the string, it returns a new string object (autoreleased, according to the naming conventions). 方法stringByReplacing ...不更改字符串,它返回一个新的字符串对象(根据命名约定自动释放)。 So after the 2nd line of code, searchText points to a totally differen NSString object. 因此,在第二行代码之后,searchText指向完全不同的NSString对象。

Besides, NSString objects cannot be changed, for that there's NSMutableString 此外,NSString对象无法更改,因为那里有NSMutableString

If you expect to modify myTextView.text , you have to write it like your second example, and assign a new value to the property you're trying to modify. 如果希望修改myTextView.text ,则必须像第二个示例一样编写它,并将新值分配给要修改的属性。 Assigning a new value to some other variable or property won't do the job - " spooky action at a distance " may work when we eventually have quantum computing, but we're not there yet. 将新值分配给其他变量或属性将无法完成任务-当最终使用量子计算时,“ 远距离的怪异动作 ”可能会起作用,但目前为止还没有。 :-) :-)

To expand a bit: Yes, searchText is a pointer. 扩大一点:是的, searchText是一个指针。 But so is myTextView.text , and when you do " searchText = myTextView.text ", you're not creating any sort of lasting relationship between the two - all you're doing is making searchText point to the same target as myTextView.text . 但是myTextView.text ,当您执行“ searchText = myTextView.text ”时,您并没有在两者之间建立任何持久的关系-您所做的只是使searchText指向与myTextView.text相同的目标。 。 Changing either one of them after that point will have no effect on the other. 在那一点之后更改其中任何一个都不会影响另一个。 So, when you assign the result of stringByReplacing... to searchText , you're making it and only it point to a different target. 所以,当您分配的结果stringByReplacing...searchText ,你让只有它指向一个不同的目标。

Your second example invokes the setter of the "text" property. 第二个示例调用“文本”属性的设置器。

Your first example takes the pointer of the string, and then changes the pointer within the same scope. 您的第一个示例使用字符串的指针,然后在相同范围内更改指针。 Hence, "text" is not changed. 因此,“文本”不变。

BTW: Depending on how your property is defined, the setter you use will either copy, retain or assign the value you give the setter. 顺便说一句:根据属性的定义,您使用的设置器将复制,保留或分配给设置器的值。 So if you use the following: 因此,如果使用以下命令:

@property(copy) NSString* text;

Then yes, the setter will copy the value you give it when you invoke: 然后,是的,设置器将在调用时复制您提供的值:

myTextArea.text = //some string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM