NSString 可能无法响应附加字符串

发布于 2024-12-03 12:50:23 字数 377 浏览 2 评论 0原文

我有一个 NSMutableString ,我正在尝试使用appendString 方法。虽然它工作正常,但每次使用它时,我都会收到警告 NSString 可能无法响应附加字符串。这是我的代码:

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    [soapResults appendString: string];        //soapResults is an NSMutableString in .h
}  

我知道由于 NSString string 我收到此警告,但如何删除此警告?

I have a NSMutableString and I am trying to use appendString method. Though it works fine but every time I use it, I receive a warning NSString may not respond to append string. This is my code :

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    [soapResults appendString: string];        //soapResults is an NSMutableString in .h
}  

I know I am getting this warning because of NSString string but how to remove this warning?

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

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

发布评论

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

评论(3

得不到的就毁灭 2024-12-10 12:50:23
self.soapResults=(NSMutableString *)[self.soapResults stringByAppendingString:string];

or

soapResults=(NSMutableString *)[soapResults stringByAppendingString:string];

或者

self.soapResults=(NSMutableString *)[[self.soapResults stringByAppendingString:string] mutableCopy];

    //    or

soapResults=(NSMutableString *)[[soapResults stringByAppendingString:string] mutableCopy];
self.soapResults=(NSMutableString *)[self.soapResults stringByAppendingString:string];

or

soapResults=(NSMutableString *)[soapResults stringByAppendingString:string];

OR

self.soapResults=(NSMutableString *)[[self.soapResults stringByAppendingString:string] mutableCopy];

    //    or

soapResults=(NSMutableString *)[[soapResults stringByAppendingString:string] mutableCopy];
生活了然无味 2024-12-10 12:50:23

如果您确定soapResults实际上是一个NSMutableString,并且只有一个警告,那么只需在调用之前将其转换即可appendString: 到它:

[(NSMutableString*)soapResults appendString: string]; 

如果您将 soapResults 声明为 NSMutableString 但实际上影响了它的 NSString,那么您可能没有警告,但会有运行时执行时出错。

如果 soapResults 是一个 NSString(因为您这样声明它,或者因为您影响了一个 NSString),您可以改为创建通过向其附加字符串来创建一个新的 NSString,然后从中创建一个 mutableCopy...但我推荐这样做,因为它会生成无用的分配(和NSMutableString 最好避免这种情况)

If you are sure soapResults is actually an NSMutableString, and only have a warning, then simply cast it before calling appendString: to it :

[(NSMutableString*)soapResults appendString: string]; 

If you declared soapResults as an NSMutableString but in fact affected an NSString to it, then you probably don't have a warning, but will have a runtime error on execution.

If soapResults is an NSString (either because you declared it so or because you affected an NSString to it), you may instead create a new NSString by appending string to it, then create a mutableCopy from it... but I don't recommend this as it will generate useless allocations (and NSMutableString is better to avoid this)

梦途 2024-12-10 12:50:23

简单地说,此时代码中的soapResults 不是一个NSMutableString。也许代码中的其他地方它被无意中替换为 NSString

顺便说一句,尽管 NSMutableStringNSString 的子类,但 NSString 实际上是一个 NSMutableString 代码。这可能有助于解释代码中发生的情况。

尽管如此,将 NSString 转换为 NSMutableString 并不是一个好主意,真正需要做的是找到它在哪里变成 NSString 并解决这个问题。

Simply, at this point in the code soapResults is not a NSMutableString. Perhaps somewhere else in the code it is inadvertently replaced with a NSString.

As an aside, NSString is really an NSMutableString code-wise even though NSMutableString is a subclass of NSString. This might help explain what is happening in the code.

Still, casting an NSString to an NSMutableString is not a good idea, what really needs to be done is find where it is becoming an NSString and fix that.

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