xcode setString NSMutableString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5346142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:33:22  来源:igfitidea点击:

setString NSMutableString

xcodecalculatornsmutablestringsetstring

提问by Jonathan Goyens

I am new to programming in XCode, and as a first familiarisation exercise, I would like to make a small Calculator app. So obviously I came upon a few problems: I try to have the user use buttons to type the digits and operators into a string, which he then sees. When I use

我是 XCode 编程的新手,作为第一个熟悉练习,我想制作一个小型计算器应用程序。很明显,我遇到了一些问题:我尝试让用户使用按钮将数字和运算符键入一个字符串,然后他会看到。当我使用

displayString = [NSString stringWithFormat:@"%@%@", displayString, operatorString];

everything works fine. Then I decided to work with NSMutableStrings in order to get deletebuttons. I changed the headerfile appropriately, after which I still the earlier NSString functionality (? because NSMutableString inherits from NSString?) And then I got the following problem:

一切正常。然后我决定使用 NSMutableStrings 来获取删除按钮。我适当地更改了头文件,之后我仍然使用较早的 NSString 功能(?因为 NSMutableString 继承自 NSString?)然后我遇到了以下问题:

[displayString setString:@"test"];
NSLog(@"%@", displayString);

Even when this is the first method I call, I still get nul. I What am I doing wrong?

即使这是我调用的第一个方法,我仍然得到空值。我做错了什么?

Unrelated, is there a way to have this string write to a sort of function: I would like to be able to do

不相关,有没有办法让这个字符串写入某种函数:我希望能够做到

result = contentof:displayString

or something a like. Any ideas in how I could do this?

或类似的东西。关于我如何做到这一点的任何想法?

回答by esqew

NSStringdoesn't have a setString:method. (Documentation)

NSString没有setString:方法。(文档

For this case, you'd need an NSMutableString. Here's how you'd implement it:

对于这种情况,您需要一个NSMutableString. 下面是你如何实现它:

NSMutableString *mutableString = [[NSMutableString alloc] initWithFormat:"%@%@", displayString, operatorString];

... and then to change it:

...然后改变它:

[mutableString setString:@"test"];