Search code examples
xcodecore-dataios6uipicker

iOS - Getting value from UIPicker, where item is a relationship


I am trying to use the value from a UIPicker in a core-data app.

The picker is filled with items from entity B (categories).

I want to have the record in entity A use the selected item from entity B.

I can load the picker, and by logging, I can see the picker values, e.g.:

cat is Insurance,

cat is Travel.

cat is a relationship to the main entity (expense), I don't know how to link the one to the other.

I've been trying some of these:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    //NSString *cat = [expense valueForKey:@"category"];
    //NSLog(@"cat is %@", cat);

    NSString *cat2 = ((SPCategory *)[allCategories objectAtIndex:row]).name;
    NSLog(@"cat2 is %@", cat2); << this works I can see the cat name.

    self.expense.category.name = ((SPCategory *)[allCategories objectAtIndex:row]).name;
    NSLog(@"category is %@", self.expense.category.name );

    //categoryTextField.rightTextField.text = [self.expense valueForKey:@"category"];

}

Thanks for any help..


Solution

  • Try self.expense.category = ((SPCategory *)[allCategories objectAtIndex:row]).