Swift: += operator broken in Xcode6-Beta5 for appending to an array of dictionaries
| Originator: | bradwill | ||
| Number: | rdar://18000913 | Date Originated: | 8/12/2014 |
| Status: | Closed | Resolved: | 8/12/2014 |
| Product: | Swift | Product Version: | Xcode6-Beta5 (6A279r) |
| Classification: | UI/Usability | Reproducible: | Always |
Summary:
The following code worked in Xcode6-Beta4 but will not compile in Xcode6-Beta5:
var arrayOfDictionaries: [ Dictionary<String, Int> ] = []
arrayOfDictionaries += ["test":0]
Steps to Reproduce:
Paste the above two lines into any Swift project in Xcode.
See AppDelegate.swift in attached sample project, ArrayAppendTest. Lines 24-26.
Expected Results:
Code should compile and add the dictionary to the array.
Actual Results:
Compile-time error:
'[Dictionary<String, Int>]' is not identical to 'UInt8'
Workaround is to use the append function:
arrayOfDictionaries.append( ["test":0] )
Version:
Xcode6-Beta5 (6A279r)
Yosemite 10.10 (14A314h)
Notes:
Configuration:
Attachments:
'ArrayAppendTest.zip' was successfully uploaded.
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!
Known behavior
This is known behavior in XCode6-Beta5 per the release notes, "The += operator on arrays only concatenates arrays, it does not append an element. This resolves ambiguity working with Any, AnyObject and related types. (17151420)"
However, there are two problems:
The compile error message is not clear at all. It should state that += cannot be used to append an item to an array.
The documentation still states this is allowed:
Alternatively, add a new item to the end of an array with the addition assignment operator (+=): shoppingList += "Baking Powder" // shoppingList now contains 4 items