NSString 可能无法响应附加字符串
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者
OR
如果您确定soapResults实际上是一个NSMutableString,并且只有一个警告,那么只需在调用之前将其转换即可
appendString:
到它:如果您将
soapResults
声明为 NSMutableString 但实际上影响了它的NSString
,那么您可能没有警告,但会有运行时执行时出错。如果
soapResults
是一个NSString
(因为您这样声明它,或者因为您影响了一个NSString
),您可以改为创建通过向其附加字符串来创建一个新的NSString
,然后从中创建一个mutableCopy
...但我不推荐这样做,因为它会生成无用的分配(和NSMutableString
最好避免这种情况)If you are sure
soapResults
is actually anNSMutableString
, and only have a warning, then simply cast it before callingappendString:
to it :If you declared
soapResults
as an NSMutableString but in fact affected anNSString
to it, then you probably don't have a warning, but will have a runtime error on execution.If
soapResults
is anNSString
(either because you declared it so or because you affected anNSString
to it), you may instead create a newNSString
by appending string to it, then create amutableCopy
from it... but I don't recommend this as it will generate useless allocations (andNSMutableString
is better to avoid this)简单地说,此时代码中的soapResults 不是一个
NSMutableString
。也许代码中的其他地方它被无意中替换为NSString
。顺便说一句,尽管
NSMutableString
是NSString
的子类,但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 aNSString
.As an aside,
NSString
is really anNSMutableString
code-wise even thoughNSMutableString
is a subclass ofNSString
. This might help explain what is happening in the code.Still, casting an
NSString
to anNSMutableString
is not a good idea, what really needs to be done is find where it is becoming anNSString
and fix that.