“NSCharacterSet”可能不会响应“-count”

发布于 2024-09-28 14:11:50 字数 198 浏览 3 评论 0原文

中收到一条警告消息?

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 技术交流群。

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

发布评论

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

评论(3

2024-10-05 14:11:50

具体来说,NSCharacterSet 实例不响应选择器count

如果您将 count 实现为类别方法(应添加前缀以避免冲突),那么您将需要包含声明它的头文件。

尝试在运行时调用此方法将引发异常(或者它可能已经作为私有方法实现,在这种情况下不会出现异常;不过,您不应该依赖于此)。

specifically, NSCharacterSet instances do not respond to the selector count.

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).

逆蝶 2024-10-05 14:11:50

NSCharacterSet 官方没有提供计数方法。然而在我的测试中,[myCharSet3 count] 实际上给出了正确的结果。所以只是为了摆脱警告:

chr_count3 = (int)[myCharSet3 performSelector:@selector(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:

chr_count3 = (int)[myCharSet3 performSelector:@selector(count)];

But I don't think it's a good idea.

旧人九事 2024-10-05 14:11:50

以下是 NSCharacterSetcount 实现。我假设 NSCharacterSet 内部使用 API 中公开的 bitmapRepresentation,并且 characterIsMember 依赖于此并且速度很快。直接对 bitmapRepresentation 数据进行指针操作可能会更快(有关如何测试成员身份的信息,请参阅 Apple 关于后一种方法的文档),但我宁愿坚持使用这个可读版本。在大多数情况下,65536 次迭代的循环并没有那么糟糕,如果你经常调用这个,你也可以在处理不可变类时缓存返回值。将“foobar”替换为您自己的前缀,以避免与 count 的私有内部实现发生冲突。

- (NSUInteger)foobar_count
{
    NSUInteger count = 0;
    for (NSUInteger i = 0; i < 8192 * 16; i ++)
    {
        if ([self characterIsMember:(unichar)i])
        {
            count ++;
        }
    }
    return count;
}

Here is an implementation of count for NSCharacterSet. I assume that NSCharacterSet internals use the bitmapRepresentation exposed in the APIs, and that characterIsMember relies on this and is fast. It is probably faster to do the pointer manipulations yourself on the bitmapRepresentation 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 of count.

- (NSUInteger)foobar_count
{
    NSUInteger count = 0;
    for (NSUInteger i = 0; i < 8192 * 16; i ++)
    {
        if ([self characterIsMember:(unichar)i])
        {
            count ++;
        }
    }
    return count;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文