“NSCharacterSet”可能不会响应“-count”
中收到一条警告消息?
NSCharacterSet *myCharSet3 = [NSCharacterSet characterSetWithCharactersInString:query3];
int chr_count3;
chr_count3=[myCharSet3 count];
我在如何修复该警告
I got a warning message in
NSCharacterSet *myCharSet3 = [NSCharacterSet characterSetWithCharactersInString:query3];
int chr_count3;
chr_count3=[myCharSet3 count];
How to fix that warning ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
具体来说,
NSCharacterSet
实例不响应选择器count
。如果您将
count
实现为类别方法(应添加前缀以避免冲突),那么您将需要包含声明它的头文件。尝试在运行时调用此方法将引发异常(或者它可能已经作为私有方法实现,在这种情况下不会出现异常;不过,您不应该依赖于此)。
specifically,
NSCharacterSet
instances do not respond to the selectorcount
.if you implemented
count
as a category method (which should be prefixed to avoid collisions), then you will need to include that header file which declares it.attempts to call this method at runtime will raise an exception (or it may already be implemented as a private method, in which case there won't be an exception; you should not rely on this, though).
NSCharacterSet 官方没有提供计数方法。然而在我的测试中,[myCharSet3 count] 实际上给出了正确的结果。所以只是为了摆脱警告:
但我认为这不是一个好主意。
NSCharacterSet does not officially provide a count method. However in my testing, [myCharSet3 count] actually gives the correct result. So just to get rid of the warning:
But I don't think it's a good idea.
以下是
NSCharacterSet
的count
实现。我假设 NSCharacterSet 内部使用 API 中公开的bitmapRepresentation
,并且characterIsMember
依赖于此并且速度很快。直接对 bitmapRepresentation 数据进行指针操作可能会更快(有关如何测试成员身份的信息,请参阅 Apple 关于后一种方法的文档),但我宁愿坚持使用这个可读版本。在大多数情况下,65536 次迭代的循环并没有那么糟糕,如果你经常调用这个,你也可以在处理不可变类时缓存返回值。将“foobar”替换为您自己的前缀,以避免与count
的私有内部实现发生冲突。Here is an implementation of
count
forNSCharacterSet
. I assume that NSCharacterSet internals use thebitmapRepresentation
exposed in the APIs, and thatcharacterIsMember
relies on this and is fast. It is probably faster to do the pointer manipulations yourself on thebitmapRepresentation
data directly (see Apple's documentation on that latter method for how to test membership), but I'd rather stick to this readable version. A loop of 65536 iterations is not that bad in most situations, and if you call this often, you could also cache the returned value when dealing with the immutable class. Replace "foobar" with your own prefix to avoid collisions with private internal implementation ofcount
.