开发者

'NSCharacterSet' may not respond to '-count'

开发者 https://www.devze.com 2023-01-21 06:00 出处:网络
I got a warning message in NSCharacterSet *myCharSet3 = [NSCharacterSet characterSetWithCharactersInString:query3];

I got a warning message in

NSCharacterSet *myCharSet3 = [NSCharacterSet characterSetWithCharactersInString:query3]; 
in开发者_StackOverflow社区t chr_count3;
chr_count3=[myCharSet3 count];

How to fix that warning ?


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


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号