使用 NSString 类型的表达式初始化 NSMutableString 的指针不兼容 - 警告

发布于 2024-12-25 12:16:02 字数 438 浏览 2 评论 0原文

大家好,我收到了这段代码,并收到警告“使用 NSString 类型的表达式初始化 NSMutableString 的指针不兼容”和内存泄漏。有人似乎知道如何纠正这个问题吗?

- (IBAction)numberClicked:(id)sender {

    UIButton * numberBtn = (UIButton*)sender;

    NSMutableString *value = (self.textField.text == nil ? [NSMutableString new] : self.textField.text );
    [value appendString:[NSString stringWithFormat:@"%d",numberBtn.tag]];

    self.textField.text = value;
}

thnaks

Hi guys i got this piece of code and got a warning "Incompatible pointer initializing NSMutableString with an expression of type NSString" and a mem leak. does anyone seem to know how to correct this?

- (IBAction)numberClicked:(id)sender {

    UIButton * numberBtn = (UIButton*)sender;

    NSMutableString *value = (self.textField.text == nil ? [NSMutableString new] : self.textField.text );
    [value appendString:[NSString stringWithFormat:@"%d",numberBtn.tag]];

    self.textField.text = value;
}

thnaks

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

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

发布评论

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

评论(1

‘画卷フ 2025-01-01 12:16:02

要消除不兼容的指针警告,您需要将 self.textField.text 的可变副本分配给 value

NSMutableString *value = (self.textField.text == nil ? [NSMutableString new] : [self.textField.text mutableCopy] );

To get rid of the incompatible pointer warning, you need to assign a mutable copy of self.textField.text to value:

NSMutableString *value = (self.textField.text == nil ? [NSMutableString new] : [self.textField.text mutableCopy] );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文