5 Dec 2009

I was running the LLVM/Clang Static Analyzer on my iPhone project today and got the following error:

"Pass-by-value argument in message expression is undefined"

That means as much to me as "blah blah blah blee is undefined" so I looked at the code that was triggering it. Here's a simplified version:

 
NSString *x;
 
if (someExpression) {
  x = someThing;
} 
 
NSDictionary *someDict = [NSDictionary dictionaryWithObjectsAndKeys: x, kSomeKey, ..., nil];

What the analyzer is saying here is that it cannot know for sure that x will be assigned a value. The fix is simple: assign x a default value while declaring it:

NSString *x = @"some default value";

Tada, error gone!

(Note: in my actual app, I was iterating over a data structure and populating the default values of user preferences in NSUserDefaults – you will probably run into this in a similar situation involving a loop.)

Add Your Comment

Spam Protection by WP-SpamFree

The “Pass-by-value argument in message expression is undefined” static analyzer error explained.

  1. A better solution is to actually declare the value to be nil. This allows for easy comparison later if you need to check whether the value has indeed been set.

    A good example is error objects, lots of cocoa calls ask for NSError** typed objects. So:

    NSError *err = nil;
    [anObject someCallThatTakesErrorPointer:&err];
    if (err != nil) {
    // We know there has been an error.
    }

    Danny Greg
  2. Thanks for that!

    Nikolay
  3. Great! Thanks for that.

    iPhan
  4. Thanks a BUNCH!!! :D

    Jim
  5. Ur suggestion really solved my problem , so thanks a lot.

    swati
  6. Thanks a lot! I ran in exactly the same kind of error, under similar circumstances as you did (declaring a variable, iterating over a collection of sorts and assigning one of the collection’s values to the variable).

    Assigning nil to the variable upon declaration (well, in that case, upon definition) solved the problem.

    Thanks a lot for your help!

    Matthias
  7. NSString *x = nil;

    Thanks.

    Petar