Binding NSArrayController to an NSMutableArray not working

I have an NSMutableArray defined as a property in my ViewController class:

@interface ViewController ()

@property NSMutableArray *tableCities;

@end

When the "Add" button is clicked, a new city is added:

NSString* filePath = @"/tmp/city_test.jpeg";
    NSDictionary *obj = @{@"image": [[NSImage alloc] initByReferencingFile:filePath],
                          @"name": @"testCity",
                          @"filePath": filePath};

[_tableCities addObject: obj];

Okay, this is fine, it is adding a new element to this NSMutableArray, now what I want is to somehow bind the "name" field of each city to my NSPopUpButton.

So in storyboard I created an NSArrayController and tried to set its "Model Key Path" to my NSMutableArray as shown below:

Then I tried to bind the NSPopUpButton to the NSArrayController as follows:

but it doesn't work either, a city is added but the NSPopUpButton isn't displaying anything, what gives?

Answered by GRALInfo in 810245022

Hi @that_aint_it_chief , probably it's not clear to me your project, but if you want to use an nsarraycontroller, you should work directly on it, without other data model.

For example, you could use this arrangement:

@interface ViewController : NSViewController

@property IBOutlet ArrayController * tableCities;
@end

...

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString* filePath = @"/tmp/city_test.jpeg";
    NSDictionary *obj = @{@"image": [[NSImage alloc] initByReferencingFile:filePath],
                              @"name": @"Milano",
                              @"filePath": filePath};
    NSDictionary *obj2 = @{@"image": [[NSImage alloc] initByReferencingFile:filePath],
                          @"name": @"Londra",
                          @"filePath": filePath};
    
    NSDictionary *obj3 = @{@"image": [[NSImage alloc] initByReferencingFile:filePath],
                          @"name": @"Torino",
                          @"filePath": filePath};
    
    [_tableCities addObjects:@[obj, obj2, obj3]];
}

@end


create an NSArrayController inside the ViewController scene you are developing and link directly to arrangeObjects and model type on inspector tab, as attached screenshots.

Bye Rob

Edit

Please ignore the last image, here's the image showing the binding of the NSPopUpButton:

problem still persists tho

Accepted Answer

Hi @that_aint_it_chief , probably it's not clear to me your project, but if you want to use an nsarraycontroller, you should work directly on it, without other data model.

For example, you could use this arrangement:

@interface ViewController : NSViewController

@property IBOutlet ArrayController * tableCities;
@end

...

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString* filePath = @"/tmp/city_test.jpeg";
    NSDictionary *obj = @{@"image": [[NSImage alloc] initByReferencingFile:filePath],
                              @"name": @"Milano",
                              @"filePath": filePath};
    NSDictionary *obj2 = @{@"image": [[NSImage alloc] initByReferencingFile:filePath],
                          @"name": @"Londra",
                          @"filePath": filePath};
    
    NSDictionary *obj3 = @{@"image": [[NSImage alloc] initByReferencingFile:filePath],
                          @"name": @"Torino",
                          @"filePath": filePath};
    
    [_tableCities addObjects:@[obj, obj2, obj3]];
}

@end


create an NSArrayController inside the ViewController scene you are developing and link directly to arrangeObjects and model type on inspector tab, as attached screenshots.

Bye Rob

never mind, you were right, thanks

@GRALInfo just a follow up question if instead of using a dictionary , I wanted to use a custom class say:


@interface City

NSString* (copy) name1;
NSString* (copy) name2;

@end

// view controller
@property NSMutableArray<Citiy*> *cities;

would the process be the same?

Binding NSArrayController to an NSMutableArray not working
 
 
Q