Static Analyzer Confused by Swift Format Issues

Originator:justatheory
Number:rdar://19181702 Date Originated:2014-12-08
Status:Open Resolved:
Product:Xcode Product Version:6.1.1 (6A2008a)
Classification:Developer Tools Reproducible:Always
 
Summary:
Consider this code:

    let postParams = NSString(
        format: "name=%@ version=%@ what=%@",
        "fred",
        NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString"),
        "whatever"
    )

Xcode marks with an error, "Extra argument in call". Or consider this:

    let postParams = NSString(
        format: "name=hi %@&account=%@&body=%@",
        NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString"),
        account,
        valueTextField.stringValue
    )

It says, “Missing argument label "locale:" in call”. It also offers to correct the `objectForInfoDictionaryKey()` call, saying, “Type 'AnyObject?' cannot be implicitly downcast to 'NSLocale'; did you mean to use 'as' to force downcast?"

It took me quite some time to figure out what the problem was: In both cases, I needed to cast the `objectForInfoDictionaryKey()` call to String. But none of the messages from Xcode knew that. It was only when I tried to assign the output of `objectForInfoDictionaryKey()` to a variable elsewhere that I figured it out.

Xcode (or the static analyzer?) seems to think that there is a parameter missing. Now, AFAICT, `objectForInfoDictionaryKey()` returns a scalar value, so I don't know why it would think it might return no value -- and I'm not sure that's what it thinks is happening.

In any event, it would be nice if, instead, Xcode gave me the same information as when I tried to assign to a variable:

    let foo = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString")

This gives a warning: “Constant 'foo' inferred to have type 'Any Object?', which may be unexpected” and then offers to fix it with "Add an explicit type annotation to silence this warning". Sure enough, all I had to do was add `as String` after the call to `objectForInfoDictionaryKey()` and the errors went away.

Steps to Reproduce:


Expected Results:


Actual Results:


Version:
Version 6.1.1 (6A2008a)

Notes:


Configuration:


Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!