Pregunta

Tengo una variable que es un CFSTRINGREF y yo realizamos un cheque para asegurarme de que no sea 1 valor específico.Si es así, quiero configurarlo en el equivalente NSString de @ ""

Aquí está el código

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

Así que en el Bloque MOSS, quiero configurarlo en "" Pero Xcode me advierte que es un tipo de puntero no válido.

¿Fue útil?

Solución

cfstringref es un objeto inmutable, por lo que debe crear una nueva instancia con un valor diferente:

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

y recuerde que necesita liberar ese valor.

Pero, dado que los tipos de CFSTRINGREF y NSSTRINGS se limpian sin cargo, puede usar NSSTRING en su código (que probablemente le facilitará la entender y apoyarlo más tarde):

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top