Question

I have a variable which is a CFStringRef and I perform a check to make sure its not 1 specific value. If it is then I want to set it to the NSString equivalent of @""

Here's the code

CFStringRef data = = CFDictionaryGetValue(dict, kABPersonAddressStateKey);

NSComparisonResult result = [(NSString *)data compare:element options:compareOptions];
if(NSOrderedAscending == result) {
    // Do something here...
}
else if (NSOrderedSame == result) {
    // Do another thing here if they match...
    data = "";
}
else {
    // Try something else...
}

So in the if else block, I want to set it to "" but Xcode warns me that it is an invalid pointer type.

Was it helpful?

Solution

CFStringRef is immutable object so you must create new instance with different value:

data = CFStringCreateWithCString (NULL, "", kCFStringEncodingUTF8);

And remember that you need to release that value.

But since CFStringRef and NSStrings types are toll-free bridged you can just use NSString in your code (that will probably makes it easier to understand and support it later):

NSString *data = (NSString*)CFDictionaryGetValue(dict, kABPersonAddressStateKey);

NSComparisonResult result = [(NSString *)data compare:element options:compareOptions];
if(NSOrderedAscending == result) {
    // Do something here...
}
else if (NSOrderedSame == result) {
    // Do another thing here if they match...
    data = @"";
}
else {
    // Try something else...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top