Search code examples
iosobjective-ccore-datauipickerview

extraneous characters in UIPickerView output to UITextField


I have a UIPickerView that I use to list data from a CoreData Entity. The data output always contains the entity's attribute name and extraneous characters that I assume are CoreData formatting; for example the following is NSLog output I get looks like:

NSLog(@"%@",roomNames);

output:
(
        {
        roomName = "Living Room";
    }
)

The following is the code I use to extract the data from the picker view:

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *resultString = [[NSString alloc] initWithFormat:@"%@",[roomNames objectAtIndex:row]];
    roomTxt.text = resultString;

}

I cannot determine where the characters come from or how to remove them from the output.

Any help would be greatly appreciated.


Solution

  • The culprit is this line.

    NSString *resultString = [[NSString alloc] initWithFormat:@"%@",[roomNames objectAtIndex:row]];
    

    %@ calls the object's descriptionWithLocale: or description: method. It wasn't intended to be used to get the string value of an object, but a description in string form. Looks like it's just logging the name/key pairs for all attributes.

    The description method is implemented by NSObject to return the class and memory address of the object, but many Cocoa and Cocoa Touch classes override it to provide more useful information. (from Working With Objects)

    You need the attribute's value. What type of object is being pulled from roomNames?