keegan3d
keegan3d

Reputation: 11285

CFStringTokenizerCopyBestStringLanguage with NULL Range

I'm trying to use CFStringTokenizerCopyBestStringLanguage. The docs say:

The range of string to use for the test. If NULL, the first few hundred characters of the string are examined.

But passing NULL yields an error:

Passing 'void *' to parameter of incompatible type 'CFRange'

What is the correct way of doing this?

NSString *language = (NSString *)CFBridgingRelease(CFStringTokenizerCopyBestStringLanguage((CFStringRef)text, NULL));

Upvotes: 3

Views: 512

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108111

It looks like an error in the documentation.

NULL is typically defined as something like

#define NULL ((void*)0)

so it's a pointer.

On the other hand CFRange is defined as

struct CFRange {
   CFIndex location;
   CFIndex length;
};
typedef struct CFRange CFRange;

so it's a struct, i.e. a non-pointer type.

A struct cannot be assigned to NULL, since they have incompatible types, therefore technically speaking a CFRange cannot be NULL.

Back to your specific problem, you may want to do something like

CFStringRef text = //your text
CFRange range = CFRangeMake(0, MIN(400, CFStringGetLength(text)));
NSString *language = (NSString *)CFBridgingRelease(CFStringTokenizerCopyBestStringLanguage(text, range));

I picked 400 since the documentation states

Typically, the function requires 200-400 characters to reliably guess the language of a string.


UPDATE

I reported the error to Apple.

Upvotes: 3

Related Questions