Skip to content

NSCopying for NSDictionary #68

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,44 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
}

public func copyWithZone(zone: NSZone) -> AnyObject {
NSUnimplemented()
if self.dynamicType === NSDictionary.self {
// NSDictionary is immutable; just return ourself
return self
} else if self.dynamicType === NSMutableDictionary.self {
// Otherwise, create a new NSDictionary object

// TODO: speed up?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically you could create a NSDictionary by returning one created via the storage directly.

var keys = [AnyObject]()
var values = [AnyObject]()
for (key, value) in self {
keys.append(key)
values.append(value)
}

return NSDictionary(objects: values, forKeys: keys as! [NSObject])
} else {
NSRequiresConcreteImplementation()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intent of the NSMutableDictionary case you have written is probably correct for here.

}
}

public func mutableCopyWithZone(zone: NSZone) -> AnyObject {
NSUnimplemented()
if self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self {
//ALWAYS create and return an NSMutableDictionary

// TODO: speed up?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again you could copy the storage directly into a NSMutableDictionary since Dictionary is mutable

var keys = [AnyObject]()
var values = [AnyObject]()
for (key, value) in self {
keys.append(key)
values.append(value)
}

return NSMutableDictionary(objects: values, forKeys: keys as! [NSObject])
} else {
NSRequiresConcreteImplementation()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again the previous branch is probably appropriate here.

}
}

public convenience init(object: AnyObject, forKey key: NSCopying) {
self.init(objects: [object], forKeys: [key as! NSObject])
}
Expand Down
30 changes: 30 additions & 0 deletions TestFoundation/TestNSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class TestNSDictionary : XCTestCase {
("test_BasicConstruction", test_BasicConstruction),
("test_ArrayConstruction", test_ArrayConstruction),
("test_enumeration", test_enumeration),
("test_NSCopying", test_NSCopying),
("test_NSMutableCopying", test_NSMutableCopying),
]
}

Expand Down Expand Up @@ -88,4 +90,32 @@ class TestNSDictionary : XCTestCase {
}
XCTAssertEqual(result, ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"])
}

func test_NSCopying() {
let dict1 : NSDictionary = ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"].bridge()
let dict2 : NSDictionary = dict1.copy() as! NSDictionary
XCTAssertEqual(dict1, dict2)
XCTAssert(dict1 === dict2, "dict1's copy should have returned self")

// NSMutableDictionary copying
let mutDict1 = NSMutableDictionary(objects: ["bar".bridge(), "bang".bridge(), "trouble".bridge()], forKeys: ["foo".bridge(), "whiz".bridge(), "toil".bridge()])
let dict4 = mutDict1.copy() as! NSDictionary
XCTAssertEqual(mutDict1, dict4)
mutDict1.setObject("bubble".bridge(), forKey: "toil".bridge())
XCTAssertNotEqual(mutDict1, dict4)
}

func test_NSMutableCopying() {
let dict1 : NSDictionary = ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"].bridge()

let mutDict1 = dict1.mutableCopy() as! NSMutableDictionary
XCTAssertEqual(dict1, mutDict1)

mutDict1.setObject("bubble".bridge(), forKey: "toil".bridge())
XCTAssertNotEqual(dict1, mutDict1)
let mutDict2 = mutDict1.mutableCopy() as! NSMutableDictionary
XCTAssertEqual(mutDict1, mutDict2)
mutDict1.setObject("baz".bridge(), forKey: "foo".bridge())
XCTAssertNotEqual(mutDict1, mutDict2)
}
}