1

我有一个 CFStringRef 变量,我执行检查以确保它不是 1 特定值。如果是,那么我想将其设置为 @"" 的 NSString 等效项

这是代码

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

所以在 if else 块中,我想将它设置为 "" 但 Xcode 警告我它是一个无效的指针类型。

4

1 回答 1

4

CFStringRef 是不可变对象,因此您必须创建具有不同值的新实例:

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

请记住,您需要释放该值。

但是由于 CFStringRef 和 NSStrings 类型是免费桥接的,你可以在你的代码中使用 NSString (这可能会让以后更容易理解和支持它):

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...
}
于 2011-05-12T12:47:19.037 回答