2

I am trying to initialize an NSMutableArray with the values of an NSArray but when I NSLog the newly created NSMutableArray all i get is (null), (null)... etc

I have no idea why this happening its super frustrating.

I set up myMutableArray in the header and @synthesize etc..

then in parserdidend delegate this is what I try to do.

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    if (dataSetToParse == @"ggf"){        
        //Filter results (ISTOY = T)
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"ISTOY",@"T"];
        NSArray *filteredArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

       NSLog(@"%@", filteredArray); //This prints correct details

        [myMutableArray setArray:filteredArray];

         NSLog(@"%@", myMutableArray); //This prints (null), (null)... etc

    //Use myMutableArray later...
    }

That pretty much sums it up.

2 Answers 2

5

I guess myMutableArray is nil?

Also, you can do this:

myMutableArray = [ [ filteredArray mutableCopy ] autorelease ] ;

(don't add the autorelease if you're under ARC)

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

7 Comments

also, if parsedDataArrayOfDictionaries and myMutableArray are members variables backing properties, I think it's better to use property accessors, i.e. self.myMutableArray
That worked.. Why is it do you think the other that the way i was trying was not working??...
When you write @property (...) NSMutableArray * myMutableArray all you're 'saying' is "this variable can contain a reference to an NSMutableArray", but you still need to assign an NSMutableArray reference to the property for it to contain one. Make sense?
It's probably to complicated to explain in comments here.. @property declares the interface for a property, i.e. "instances of this class have a property called x", but it doesn't actually say how that property is implemented. Adding @synthesize creates a getter/setter implementation for a property, and also declares that the getter/setter store property's value is stored in a certain member variable.
so when you write myMutableArray instead of self.myMutableArray you are going straight to the member variable that stores the value for property myMutableArray rather then invoking the property's getter and setter
|
2

As nielsbot said, or:

myMutableArray = [NSMutableArray arrayWithArray: filteredArray];

This already returns an autoreleased instance, if you need to worry about that.

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.