简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM