Removing Assets Challenge: My Solution

I felt the instructions for this challenge were pretty vague as to what exactly they expected us to do. The only hint that I was the method name, removeAsset: has the colon, signifying that it should take a parameter. So, I interpreted that to be a pointer to an asset currently held by the BNREmployee. But the for loop that fills the employees assets doesn’t track the assets at all, so I did a quick and dirty proof of concept.

Here’s the addition to main(). I admit that it’s not the most elegant looking code. There are square brackets everywhere!

BNREmployee *randomEmployee = employees[(NSUInteger)arc4random() % employees.count];
NSLog(@"Removing asset from Employee %lu", [employees indexOfObject:randomEmployee]);
[randomEmployee removeAsset:[randomEmployee.assets objectAtIndex:0]];

I use arc4random() to get an actual random value (random() wasn’t random at all for me). An employee is selected at random, and then the first asset they own is removed via the method objectAtIndex:. In the NSLog() I get the index of that employee object in the NSArray and print it so we know which employee we’re talking about.

And its corresponding output:

2014-12-13 23:27:28.366 18 BMITime[18155:3598653] Employees: (
    "<Employee 0: $486 in assets>",
    "<Employee 1: $0 in assets>",
    "<Employee 2: $469 in assets>",
    "<Employee 3: $0 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 5: $0 in assets>",
    "<Employee 6: $870 in assets>",
    "<Employee 7: $1570 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 9: $870 in assets>"
2014-12-13 23:27:28.368 18 BMITime[18155:3598653] Removing asset from Employee 0
2014-12-13 23:27:28.368 18 BMITime[18155:3598653] deallocating <Laptop 8: $486>

You can see that Employee 0, which was randomly selected, has $486 in assets, and the asset–in this case, a laptop–is removed.

Is there more to this code?