Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3286] Dictionary element not removed in case of the value nil assignment #45874

Closed
swift-ci opened this issue Nov 28, 2016 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. not a bug Resolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer error regression standard library Area: Standard library umbrella swift 3.0

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3286
Radar None
Original Reporter Śliwiński (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment
Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug, 3.0Regression, TypeChecker
Assignee None
Priority Medium

md5: 94f717bbadb9abaae2d591688134e5cf

is duplicated by:

  • SR-15010 Settings Nil as Dictionary Value Produces Inconsistent Behavior

Issue Description:

Description:

As documentation statements, subscript method of the Dictionary should remove element when nil value is assigned to the key.

/// If you assign `nil` as the value for the given key, the dictionary
/// removes that key and its associated value.

Test case:

import Foundation
var d: [String: Any?] = ["key": nil]
d.count // 1
d["key"] = nil as Any?
// Commented lines below produce the same effect
// let x: Any? = nil
// d["key"] = x 
d["key"] // nil
d.count // 1

Expected result:

d.count result in the last line of code should be equal to 0

Actual result:

d.count result in the last line of code is equal to 1

Additional informations:

1: Without using type Any? for nil value, code works as expected

import Foundation
var d: [String: Any?] = ["key": nil]
d.count // 1
d["key"] = nil // Removed `as Any?` fix the problem
d["key"] // nil
d.count // 0

2: Using NSMutableDictionary instead of Dictionary works as expected

import Foundation
var d: [String: Any?] = ["key": nil]
var n =  NSMutableDictionary(dictionary: d)
n.count // 1
n["key"] = nil as Any? // Even with casting to `Any?` works correctly
n["key"] // nil
n.count // 0
@belkadan
Copy link
Contributor

belkadan commented Dec 9, 2016

This is correct behavior. If you want to nil out a key from a dictionary containing Any? elements, you need to use the type Any??.

@belkadan
Copy link
Contributor

belkadan commented Dec 9, 2016

See this blog post on developer.apple.com for more information: https://developer.apple.com/swift/blog/?id=12

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added not a bug Resolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer error and removed type checker Area → compiler: Semantic analysis labels Dec 13, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. not a bug Resolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer error regression standard library Area: Standard library umbrella swift 3.0
Projects
None yet
Development

No branches or pull requests

3 participants