[RemoveObjectAtIndex:5]


#1

Why do you need to remove the object at index 5? Would it not be better to remove the object at index 1? It seems more logical to me.


#2

Hi EastProgrammer,

Maybe it’s to show us how the object being deallocated.

This is the normal code:

NSLog(@"Employees: %@", employees);
        NSLog(@"Giving up ownership of one employee");
        [employees removeObjectAtIndex:5];
        NSLog(@"Giving up ownership of array");
        employees = nil;

And this is the result

2014-05-03 10:24:45.057 Redo-BMITime[551:303] Employees: (
    "<Employee 0: $0 in assets>",
    "<Employee 1: $153 in assets>",
    "<Employee 2: $119 in assets>",
    "<Employee 3: $68 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 5: $136 in assets>",
    "<Employee 6: $119 in assets>",
    "<Employee 7: $34 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 9: $136 in assets>"
)
2014-05-03 10:24:45.059 Redo-BMITime[551:303] Giving up ownership of one employee
2014-05-03 10:24:45.059 Redo-BMITime[551:303] Giving up ownership of array
2014-05-03 10:24:45.060 Redo-BMITime[551:303] deallocating <Employee 0: $0 in assets>
2014-05-03 10:24:45.060 Redo-BMITime[551:303] deallocating <Employee 1: $153 in assets>
2014-05-03 10:24:45.061 Redo-BMITime[551:303] deallocating <Laptop 9: $153 >
2014-05-03 10:24:45.061 Redo-BMITime[551:303] deallocating <Employee 2: $119 in assets>
2014-05-03 10:24:45.062 Redo-BMITime[551:303] deallocating <Laptop 7: $119 >
2014-05-03 10:24:45.062 Redo-BMITime[551:303] deallocating <Employee 3: $68 in assets>
2014-05-03 10:24:45.063 Redo-BMITime[551:303] deallocating <Laptop 0: $0 >
2014-05-03 10:24:45.063 Redo-BMITime[551:303] deallocating <Laptop 4: $68 >
2014-05-03 10:24:45.063 Redo-BMITime[551:303] deallocating <Employee 4: $0 in assets>
2014-05-03 10:24:45.064 Redo-BMITime[551:303] deallocating <Employee 5: $136 in assets>
2014-05-03 10:24:45.064 Redo-BMITime[551:303] deallocating <Laptop 3: $51 >
2014-05-03 10:24:45.065 Redo-BMITime[551:303] deallocating <Laptop 5: $85 >
2014-05-03 10:24:45.066 Redo-BMITime[551:303] deallocating <Employee 6: $119 in assets>
2014-05-03 10:24:45.066 Redo-BMITime[551:303] deallocating <Laptop 1: $17 >
2014-05-03 10:24:45.067 Redo-BMITime[551:303] deallocating <Laptop 6: $102 >
2014-05-03 10:24:45.067 Redo-BMITime[551:303] deallocating <Employee 7: $34 in assets>
2014-05-03 10:24:45.068 Redo-BMITime[551:303] deallocating <Laptop 2: $34 >
2014-05-03 10:24:45.068 Redo-BMITime[551:303] deallocating <Employee 8: $0 in assets>
2014-05-03 10:24:45.068 Redo-BMITime[551:303] deallocating <Employee 9: $136 in assets>
2014-05-03 10:24:45.069 Redo-BMITime[551:303] deallocating <Laptop 8: $136 >
Program ended with exit code: 0

This is the code with the object 5 removed:

NSLog(@"Employees: %@", employees);
        NSLog(@"Giving up ownership of one employee");
        //[employees removeObjectAtIndex:5];
        NSLog(@"Giving up ownership of array");
        employees = nil;

And this is the results:

2014-05-03 10:25:07.075 Redo-BMITime[566:303] Employees: (
    "<Employee 0: $0 in assets>",
    "<Employee 1: $153 in assets>",
    "<Employee 2: $119 in assets>",
    "<Employee 3: $68 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 5: $136 in assets>",
    "<Employee 6: $119 in assets>",
    "<Employee 7: $34 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 9: $136 in assets>"
)
2014-05-03 10:25:07.080 Redo-BMITime[566:303] Giving up ownership of one employee
2014-05-03 10:25:07.082 Redo-BMITime[566:303] deallocating <Employee 5: $136 in assets>
2014-05-03 10:25:07.083 Redo-BMITime[566:303] deallocating <Laptop 3: $51 >
2014-05-03 10:25:07.084 Redo-BMITime[566:303] deallocating <Laptop 5: $85 >
2014-05-03 10:25:07.087 Redo-BMITime[566:303] Giving up ownership of array
2014-05-03 10:25:07.088 Redo-BMITime[566:303] deallocating <Employee 0: $0 in assets>
2014-05-03 10:25:07.089 Redo-BMITime[566:303] deallocating <Employee 1: $153 in assets>
2014-05-03 10:25:07.089 Redo-BMITime[566:303] deallocating <Laptop 9: $153 >
2014-05-03 10:25:07.090 Redo-BMITime[566:303] deallocating <Employee 2: $119 in assets>
2014-05-03 10:25:07.091 Redo-BMITime[566:303] deallocating <Laptop 7: $119 >
2014-05-03 10:25:07.091 Redo-BMITime[566:303] deallocating <Employee 3: $68 in assets>
2014-05-03 10:25:07.092 Redo-BMITime[566:303] deallocating <Laptop 0: $0 >
2014-05-03 10:25:07.093 Redo-BMITime[566:303] deallocating <Laptop 4: $68 >
2014-05-03 10:25:07.093 Redo-BMITime[566:303] deallocating <Employee 4: $0 in assets>
2014-05-03 10:25:07.095 Redo-BMITime[566:303] deallocating <Employee 6: $119 in assets>
2014-05-03 10:25:07.096 Redo-BMITime[566:303] deallocating <Laptop 1: $17 >
2014-05-03 10:25:07.097 Redo-BMITime[566:303] deallocating <Laptop 6: $102 >
2014-05-03 10:25:07.098 Redo-BMITime[566:303] deallocating <Employee 7: $34 in assets>
2014-05-03 10:25:07.099 Redo-BMITime[566:303] deallocating <Laptop 2: $34 >
2014-05-03 10:25:07.100 Redo-BMITime[566:303] deallocating <Employee 8: $0 in assets>
2014-05-03 10:25:07.100 Redo-BMITime[566:303] deallocating <Employee 9: $136 in assets>
2014-05-03 10:25:07.101 Redo-BMITime[566:303] deallocating <Laptop 8: $136 >
Program ended with exit code: 0

This is the code with Object 0 removed:

   NSLog(@"Employees: %@", employees);
        NSLog(@"Giving up ownership of one employee");
        [employees removeObjectAtIndex:0];
        NSLog(@"Giving up ownership of array");
        employees = nil;

And this is the result:

2014-05-03 10:28:54.070 Redo-BMITime[582:303] Employees: (
    "<Employee 0: $0 in assets>",
    "<Employee 1: $153 in assets>",
    "<Employee 2: $119 in assets>",
    "<Employee 3: $68 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 5: $136 in assets>",
    "<Employee 6: $119 in assets>",
    "<Employee 7: $34 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 9: $136 in assets>"
)
2014-05-03 10:28:54.073 Redo-BMITime[582:303] Giving up ownership of one employee
2014-05-03 10:28:54.073 Redo-BMITime[582:303] deallocating <Employee 0: $0 in assets>
2014-05-03 10:28:54.074 Redo-BMITime[582:303] Giving up ownership of array
2014-05-03 10:28:54.074 Redo-BMITime[582:303] deallocating <Employee 1: $153 in assets>
2014-05-03 10:28:54.075 Redo-BMITime[582:303] deallocating <Laptop 9: $153 >
2014-05-03 10:28:54.075 Redo-BMITime[582:303] deallocating <Employee 2: $119 in assets>
2014-05-03 10:28:54.076 Redo-BMITime[582:303] deallocating <Laptop 7: $119 >
2014-05-03 10:28:54.076 Redo-BMITime[582:303] deallocating <Employee 3: $68 in assets>
2014-05-03 10:28:54.077 Redo-BMITime[582:303] deallocating <Laptop 0: $0 >
2014-05-03 10:28:54.077 Redo-BMITime[582:303] deallocating <Laptop 4: $68 >
2014-05-03 10:28:54.078 Redo-BMITime[582:303] deallocating <Employee 4: $0 in assets>
2014-05-03 10:28:54.078 Redo-BMITime[582:303] deallocating <Employee 5: $136 in assets>
2014-05-03 10:28:54.079 Redo-BMITime[582:303] deallocating <Laptop 3: $51 >
2014-05-03 10:28:54.079 Redo-BMITime[582:303] deallocating <Laptop 5: $85 >
2014-05-03 10:28:54.080 Redo-BMITime[582:303] deallocating <Employee 6: $119 in assets>
2014-05-03 10:28:54.081 Redo-BMITime[582:303] deallocating <Laptop 1: $17 >
2014-05-03 10:28:54.081 Redo-BMITime[582:303] deallocating <Laptop 6: $102 >
2014-05-03 10:28:54.082 Redo-BMITime[582:303] deallocating <Employee 7: $34 in assets>
2014-05-03 10:28:54.082 Redo-BMITime[582:303] deallocating <Laptop 2: $34 >
2014-05-03 10:28:54.082 Redo-BMITime[582:303] deallocating <Employee 8: $0 in assets>
2014-05-03 10:28:54.083 Redo-BMITime[582:303] deallocating <Employee 9: $136 in assets>
2014-05-03 10:28:54.083 Redo-BMITime[582:303] deallocating <Laptop 8: $136 >
Program ended with exit code: 0

We can see that if the object 0 is removed, the result is not so different with when there is no object removed. While if we remove object 5, we can immediately see the result that object 5 is deallocated earlier than any other object (because eventually all objects will be deallocated when the program is terminated).