I was running the LLVM/Clang Static Analyzer on my iPhone project today and got the following error:
The The “Pass-by-value argument in message expression is undefined” static analyzer error explained. article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.
"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.)
The The “Pass-by-value argument in message expression is undefined” static analyzer error explained. article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.
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.
}
Thanks for that!
Great! Thanks for that.
Thanks a BUNCH!!! :D
Ur suggestion really solved my problem , so thanks a lot.
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!
NSString *x = nil;
Thanks.