Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Removing an object from an Array in another class

Hi,

Anyone know how I remove an object from an array in another class? I've imported the .h file but I don't know how to access the array

The array is

@property (nonatomic, strong) NSMutableArray *friends; and it sits in FriendsTableViewController.h

When I edit my friends (in another class) I want it to update this Array

Please can someone help! Thanks Ryann

4 Answers

Oh wait, you said remove from array. Well if all you're doing is removing then replace this method

- (void)updateArray:(NSNotification *)notification
{
    NSUInteger index = [notification.userInfo[@"index"] integerValue];

    if (index < _friends.count) {  // prevent crash
        [_friends removeObjectAtIndex:index];
    }

    NSLog(@"_friends : %@", _friends);
}

And in the view controller that is changing the array add...

    // Put this code in whatever is triggering the update of the array.
    // i.e [_friends addObject:@"Some Friends"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateArray"
                                                    object:nil
                                                  userInfo:@{@"index" : @(2)}]; // note its NSNumber because you can't store primitive types in an dictionary

Amit Bijlani Please could you give me a hand. I'm really struggling!

I'm not sure if this is the best/cleanest way but I would just use a NSNotification. On the viewController that you are wanting to receive the update of the friends array then add this in the viewDidLoad. This will listen for a notification sent for when you want to update the array.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateArray:) // the method that updates the array
                                                 name:@"UpdateArray" // this name can be whatever you want to call it just make sure it matches the name in your postNotification method in the other viewController
                                               object:nil]; // leave nil in this case since you don't care what object is listening
    _friends = [NSMutableArray array];
}

- (void)updateArray:(NSNotification *)notification
{
    _friends = [NSMutableArray arrayWithArray:notification.userInfo[@"array"]];
    NSLog(@"_friends : %@", _friends);
}

And in the view controller that is changing the array add...

    // Put this code in whatever is triggering the update of the array.
    // i.e [_friends addObject:@"Some Friends"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateArray"
                                                        object:nil
                                                      userInfo:@{@"array" : _array.copy }];

Thanks! That's amazing!!

Cheers Ryann