-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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? | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) | ||
} | ||
|
There was a problem hiding this comment.
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.