Forced Unwrapping of Dictionary or Array Cannot Modify Elements

Originator:lukeben92
Number:rdar://17191446 Date Originated:6/5/2014
Status:Duplicate Resolved:6/10/2014
Product:Swift Product Version:
Classification: Reproducible:Always
 
UPDATE 6/10/2014: Marked as duplicate of 17183596

Forced unwrapping of a dictionary or array cannot add/modify/remove elements from the dictionary or array without assigning the unwrapped value to a variable or constant.

Example:

Every single access after the ! below has a compile time "could not find member" error.

var optionalDict: Dictionary<String, String>? = Dictionary<String, String>()
optionalDict!.removeValueForKey("test")
optionalDict!.removeAll(keepCapacity: true)
optionalDict!.updateValue("test", forKey: "test")

var optionalArray: Int[]? = Int[]()
optionalArray!.removeAtIndex(2)
optionalArray!.append(4)

However, all of the methods work if the unwrapped values are assigned to another variable or constant, like this:

var dict = optionalDict!
dict.removeValueForKey("test")
dict.removeAll(keepCapacity: true)
dict.updateValue("test", forKey: "test")

var arr = optionalArray!
arr.removeAtIndex(2)
arr.append(4)

Steps to Reproduce:
1. Install Xcode
2. Open a new playground
3. Try the example code from the description

Expected Results:
Be able to unwrap and use an optional array or dictionary

Actual Results:
Cannot unwrap and use an optional array or dictionary without first assigning the unwrapped value to another variable or constant

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!