Search code examples
cocoacore-datacocoa-bindingsnsarraycontrollerkvc

Model Key Path With Multiple Properties


Here, I'm using an NSArrayController to bind properties from Core Data entities into the value of text view table cells.

enter image description here

What syntax do I use to access multiple properties of the entity in the Model Key Path?

Example as a format string: @"%@, %@", lastName, firstName;


Solution

  • In the bindings inspector for the text field:

    1. Select "Value With Pattern: Display Pattern Value1"
    2. Bind to the array controller
    3. Controller key = selection
    4. Model key path = lastName
    5. Display pattern = %{value1}@, %{value2}@
    6. Select the now available in bindings inspector: "Display Pattern Value2"
    7. Bind Value2 to AC, selection, firstName

    This is documented in the NSTextField section of the Cocoa Bindings Reference.

    For completeness, here is some of my own code where I do this sort of binding programmatically:

    NSString* bannerPattern = @": %{value1}@ items found, %{value2}@ hidden %{value3}@";
    NSString* totalPattern = [dateString stringByAppendingString:bannerPattern];
    
    [ftview.textField bind:@"displayPatternValue1" toObject:ft withKeyPath:@"visibleNumber" options:@{NSDisplayPatternBindingOption : totalPattern}];
    [ftview.textField bind:@"displayPatternValue2" toObject:ft withKeyPath:@"hiddenNumber" options:@{NSDisplayPatternBindingOption : totalPattern}];
    [ftview.textField bind:@"displayPatternValue3" toObject:ft withKeyPath:@"newString" options:@{NSDisplayPatternBindingOption : totalPattern}];