Cocoa Dev
Cocoa Dev

Reputation: 9541

CFStringRef setting a value to ""

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.

Upvotes: 1

Views: 1804

Answers (1)

Vladimir
Vladimir

Reputation: 170839

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

Upvotes: 4

Related Questions