NSMutableString setString

发布于 2024-10-24 08:46:50 字数 601 浏览 3 评论 0原文

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

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

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

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

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

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

result = contentof:displayString

或类似的事情。 我有什么想法可以做到这一点吗?

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

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:

[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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-10-31 08:46:50

NSString 没有 setString: 方法。 (文档

对于这种情况,您需要一个 NSMutableString。以下是实现它的方法:

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

...然后更改它:

[mutableString setString:@"test"];

NSString doesn't have a setString: method. (Documentation)

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

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

... and then to change it:

[mutableString setString:@"test"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文