1

When converting NSString to constants, I usually use

[@"..." UTF8String];

I was just looking over apple docs on address book programming and i see they use the macro

CFSTR("...");

Out of curiousity, I'm just wondering is there any different between the two?

3 Answers 3

5

CFSTR("...") is essentially the Core Foundation equivalent of @"...". In fact, you can do the following:

const char *utf8dots = [(NSString *)CFSTR("...") UTF8String];

And you'll get the same result as you would with your first line of code.

In theory, CFSTR() creates a constant CFString, while @"" creates a constant NSString. In practice, the two types are interchangeable.

Does that help?

Sign up to request clarification or add additional context in comments.

2 Comments

so basically, there is no practical difference in the value returned by CFSTR("foo") and the value returned by [@"foo" UTFTString] ?
No, re-read. There's no practical difference between CFSTR("foo") and @"foo". -UTF8String can be sent to either and returns a third type of string, a C string encoded as UTF-8. That string isn't an object, it's a bare C pointer.
3

Those create completely different sorts of things. One gets you a CFStringRef, which is an object (well, a CFType, but that's compatible with objects). The other gets you a char*, which is just an array of chars, and not an object at all. You could, for example, do this:

[(NSString *)CFSTR("foo") UTF8String];

and that would be totally valid.

2 Comments

so basically, there is no practical difference in the value returned by CFSTR("foo") and the value returned by [@"foo" UTFTString] ?
That's the opposite of what I said. There's no practical difference between CFSTR("foo") and @"foo". There's a HUGE difference between either of those, and the return value of -UTF8String
1

The former doesn't actually create anything, it simply returns an internal const char pointer of an NSString (run-time function). The latter will create a compile-time constant CFStringRef value. It cannot be used with variables because those variables will not be available at compile time.

EDIT I realize that last sentence is a bit vague. You can use the result of CFSTR as a variable, but you cannot pass a variable as an argument.

2 Comments

What do you mean by "it cannot be used with variables" here?
If you try to insert a variable instead of a literal string you will get an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.