0

If I instantiate an NSArray passing it an NSMutableArray, does it become a NSArray or does it just appear to be one.

i.e. is the mutable array eventually released

- (NSArray *)getObjectivesWithPerspective:(Perspective *)perspective
{
    NSMutableArray *result = [NSMutableArray array];

    for (ObjectiveManagedObject *objective in self.objectives)
    {
        if (objective.perspective.objectID == perspective.objectID) {
            [result addObject:objective];
        }
    }

    return [NSArray arrayWithArray:result];
}
0

3 Answers 3

1

See this answer: https://stackoverflow.com/a/1392051/254422

Basically, it copies the objects into a new, autoreleased, immutable NSArray.

The NSMutableArray created using +array is autoreleased.

Sign up to request clarification or add additional context in comments.

4 Comments

You might want to reread the Advanced Memory Management Programming Guide -- the rule you stated is incorrect. According to the guide, "You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy)."
You might want to read the NSArray class reference. +arrayWithArray: "Creates and returns an array containing the objects in another given array." Just because the particular static method +arrayWithArray: does not begin with alloc, new, copy or mutableCopy, does not mean that any underlying code beneath it does not.
Actually, Objective-C doesn't have static methods; it has class methods which are something quite different. Not sure what relevance you think the NSArray class reference has to the naming rules for memory management, but your statement regarding "methods not starting with new/create or init" is flat-out wrong.
Ah, I thought you were arguing that the NSArray returned from +arrayWithArray: was not a new object just because it wasn't called with alloc, new, copy or mutableCopy. I have updated the answer.
0

You can find some info about this here: what does -arrayWithArray actually DO?. But the array you get back should be immutable. NSArray will copy the references into an immutable array.

If in doubt do a test. Get an array back from the method, cast it as a NSMutableArray and try to add an object. You should get an error when you do this.

Comments

0

As NSMutableArray is a subclass of NSArray, you can return result directly without the need to create a new NSArray object:

- (NSArray *)getObjectivesWithPerspective:(Perspective *)perspective
{
    NSMutableArray *result = [NSMutableArray array];

    for (ObjectiveManagedObject *objective in self.objectives)
    {
        if (objective.perspective.objectID == perspective.objectID) {
            [result addObject:objective];
        }
    }

    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.