views:

227

answers:

1

I am fairly new to Objective-C and whilst running the Clang static analyser this section of code gave me the following error

warning: Pass-by-value argument in message expression is undefined
[artistCollection removeObject:removeArtist];

Can anyone cast any light on this warning for me?

case 6:
    NSLog(@"(*) - First Name:");
    scanf("%s", userFirName);
    objFirName = [[NSString alloc] initWithUTF8String:userFirName];
    for(eachArtist in artistCollection) {
        if([[eachArtist firName] isEqualToString: objFirName]) {
            removeArtist = eachArtist;
        }
    }
    [artistCollection removeObject:removeArtist];
    [objFirName release], objFirName = nil;
    break;

gary

+3  A: 

If you never get a match on that if inside your loop (because userFirName isn't in your collection), removeArtist will never get assigned a value. Assign it a value before starting the loop (nil, probably), and you should be fine.

Carl Norum
Thank you, a silly mistake, still trying to get to grips with this. Two questions, it is my understand that it is bad to delete from within the for loop, which is why I delete the object outside. Am I right in thinking that? Finally, should I have a [removeArtist release] in there? looking at it now it feels like I do.
fuzzygoat
Yes, if you try to modify the array while you're in the loop, you'll get an exception, according to the docs.I don't know if you need a `[removeArtist release]` in there; the collection will release it when you call `removeObject:`, so you don't need to do it yourself unless you retained it elsewhere for some reason (I would expect probably not).
Carl Norum
The aforementioned document snippet: "Enumeration is 'safe'—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised." - http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocFastEnumeration.html#//apple_ref/doc/uid/TP30001163-CH18-SW1
Carl Norum
Thanks Carl, much appreciated.
fuzzygoat