문제

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...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top