2

I came to notice that executing a for/in operation in objective c on an initialized empty NSMutableArray was not working as expected.

Simplified code is :

+(void) convertArray: (NSMutableArray*)arrayIN {

    NSMutableArray *arrayOUT = [NSMutableArray array];

    NSLog(@"is nil %d - count %d", !arrayIN, [arrayIN count]);

    for(NSObject *o in arrayIN)
        [arrayOUT addObject:[o convertToAnotherClass]];

}

Actual code is :

+(BOOL) writeTasks: (NSArray*)tasksArray {

    NSMutableArray *arr = [NSMutableArray array];
    NSLog(@"is nil %d - count %d", !arr, [arr count]);
    for(Task *t in tasksArray)
        [arr addObject:[t getDictionary]];

    NSError *error; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];

    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
        return NO;
    } else {
        //NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        [jsonData writeToFile:path options:NSDataWritingAtomic error:nil];
        return YES;
    }
}

the suprising thing is that executing [dummyClass convertArray:[NSMutableArray array]] is showing this :

2012-06-25 13:51:34.236 Planorama[740:707] is nil 0 - count 0
2012-06-25 13:51:34.239 Planorama[740:707] -[__NSArrayM convertToAnotherClass]: unrecognized selector sent to instance 0xde9b580
(lldb) 

Why ? arrayIN is empty, why is convertToAnotherClass even called ?

2
  • Your code doesn't match what you're saying. You say that arrayIN is empty, but your NSLog refers to arr which is not mentioned at all in your example? Commented Jun 25, 2012 at 12:24
  • Oops, I tried to give explicits name bo forgot some of them... Commented Jun 25, 2012 at 12:31

2 Answers 2

1

if you use the block based enumeration it will work the way you want. Also, the output indicates that o is set to some instance of something, so you may have another problem.

Elegant way to get all objects of a specific type in an Objective-C array

Lastly, it looks like this is a static method, but your example calls it as an instance method.

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

3 Comments

I may have miswritten it but this is called correctly and compiles without a problem. The only problem I have is that even an empty array declared using [NSMutableArray array] and showing a count of 0 is iterated..
I pasted the actual code. I thought it might be easier to simplify it, but it adds too much questions. So here it is !
yes, you are checking the length of the mutable array but iterating over the taskArray, which is passed in. the newly instantiated mutable array is, of course, 0 length but the taskArray argument appears to have length > 0 in this debugging output.
0

As Joshua Smith pointed out : I am not checking the count of the iterated array. The iterated array was not empty and contained itself because if a mistyped line :

[tasks addObject:tasks]

instead of

[tasks addObject:task]

in a previous method..

Thanks everyone !

PS : the link of Joshua Smith is very useful ! Future readers : check it out !

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.