Skip to content

Commit 9c1f08c

Browse files
authored
Merge pull request #2077 from spevans/pr_nsset_init
NSSet: Add missing init(set: NSSet) and .customMirror
2 parents 8b34a28 + aebde06 commit 9c1f08c

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

Foundation/NSSet.swift

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,43 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
4949
_storage.insert(__SwiftValue.store(obj))
5050
}
5151
}
52-
52+
53+
public convenience init(array: [Any]) {
54+
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: array.count)
55+
for (idx, element) in array.enumerated() {
56+
buffer.advanced(by: idx).initialize(to: __SwiftValue.store(element))
57+
}
58+
self.init(objects: buffer, count: array.count)
59+
buffer.deinitialize(count: array.count)
60+
buffer.deallocate()
61+
}
62+
63+
public convenience init(set: Set<AnyHashable>) {
64+
self.init(set: set, copyItems: false)
65+
}
66+
67+
public convenience init(set anSet: NSSet) {
68+
self.init(array: anSet.allObjects)
69+
}
70+
71+
public convenience init(set: Set<AnyHashable>, copyItems flag: Bool) {
72+
if flag {
73+
self.init(array: set.map {
74+
if let item = $0 as? NSObject {
75+
return item.copy()
76+
} else {
77+
return $0
78+
}
79+
})
80+
} else {
81+
self.init(array: Array(set))
82+
}
83+
}
84+
85+
public convenience init(object: Any) {
86+
self.init(array: [object])
87+
}
88+
5389
public required convenience init?(coder aDecoder: NSCoder) {
5490
guard aDecoder.allowsKeyedCoding else {
5591
preconditionFailure("Unkeyed coding is unsupported.")
@@ -167,44 +203,6 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
167203
return self.count
168204
}
169205

170-
public convenience init(array: [Any]) {
171-
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: array.count)
172-
for (idx, element) in array.enumerated() {
173-
buffer.advanced(by: idx).initialize(to: __SwiftValue.store(element))
174-
}
175-
self.init(objects: buffer, count: array.count)
176-
buffer.deinitialize(count: array.count)
177-
buffer.deallocate()
178-
}
179-
180-
public convenience init(set: Set<AnyHashable>) {
181-
self.init(set: set, copyItems: false)
182-
}
183-
184-
public convenience init(set: Set<AnyHashable>, copyItems flag: Bool) {
185-
if flag {
186-
self.init(array: set.map {
187-
if let item = $0 as? NSObject {
188-
return item.copy()
189-
} else {
190-
return $0
191-
}
192-
})
193-
} else {
194-
self.init(array: Array(set))
195-
}
196-
}
197-
}
198-
199-
extension NSSet {
200-
201-
public convenience init(object: Any) {
202-
self.init(array: [object])
203-
}
204-
}
205-
206-
extension NSSet {
207-
208206
open var allObjects: [Any] {
209207
if type(of: self) === NSSet.self || type(of: self) === NSMutableSet.self {
210208
return _storage.map { __SwiftValue.fetch(nonOptional: $0) }
@@ -346,8 +344,10 @@ extension NSSet : Sequence {
346344
}
347345
}
348346

349-
extension NSSet : CustomReflectable {
350-
public var customMirror: Mirror { NSUnimplemented() }
347+
extension NSSet: CustomReflectable {
348+
public var customMirror: Mirror {
349+
return Mirror(reflecting: self._storage)
350+
}
351351
}
352352

353353
open class NSMutableSet : NSSet {

TestFoundation/TestNSSet.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ class TestNSSet : XCTestCase {
3535
let set2 = NSSet(array: ["foo", "bar"])
3636
XCTAssertEqual(set.count, 0)
3737
XCTAssertEqual(set2.count, 2)
38+
39+
let set3 = NSMutableSet(capacity: 3)
40+
set3.add(1)
41+
set3.add("foo")
42+
let set4 = NSSet(set: set3)
43+
XCTAssertEqual(set3, set4)
44+
set3.remove(1)
45+
XCTAssertNotEqual(set3, set4)
46+
47+
let set5 = NSMutableSet(set: set3)
48+
XCTAssertEqual(set5, set3)
49+
set5.add(2)
50+
XCTAssertNotEqual(set5, set3)
3851
}
3952

4053
func testInitWithSet() {

0 commit comments

Comments
 (0)