Skip to content

Commit 7556219

Browse files
committed
NSArray: Add missing init(array: NSArray) initializer.
- Group all of the init() methods together.
1 parent 89de36e commit 7556219

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

Foundation/NSArray.swift

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import CoreFoundation
1212
open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCoding {
1313
private let _cfinfo = _CFInfo(typeID: CFArrayGetTypeID())
1414
internal var _storage = [AnyObject]()
15-
15+
1616
open var count: Int {
1717
guard type(of: self) === NSArray.self || type(of: self) === NSMutableArray.self else {
1818
NSRequiresConcreteImplementation()
@@ -74,7 +74,38 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
7474
self.init(array: objects)
7575
}
7676
}
77-
77+
78+
public convenience init(object anObject: Any) {
79+
self.init(array: [anObject])
80+
}
81+
82+
public convenience init(array: [Any]) {
83+
self.init(array: array, copyItems: false)
84+
}
85+
86+
public convenience init(array: NSArray) {
87+
self.init(array: array as [AnyObject], copyItems: true)
88+
}
89+
90+
public convenience init(array: [Any], copyItems: Bool) {
91+
92+
let optionalArray : [AnyObject] =
93+
copyItems ?
94+
array.map { return __SwiftValue.store($0).copy() as! NSObject } :
95+
array.map { return __SwiftValue.store($0) }
96+
97+
// This would have been nice, but "initializer delegation cannot be nested in another expression"
98+
// optionalArray.withUnsafeBufferPointer { ptr in
99+
// self.init(objects: ptr.baseAddress, count: array.count)
100+
// }
101+
let cnt = array.count
102+
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: cnt)
103+
buffer.initialize(from: optionalArray, count: cnt)
104+
self.init(objects: buffer, count: cnt)
105+
buffer.deinitialize(count: cnt)
106+
buffer.deallocate()
107+
}
108+
78109
open func encode(with aCoder: NSCoder) {
79110
guard aCoder.allowsKeyedCoding else {
80111
preconditionFailure("Unkeyed coding is unsupported.")
@@ -124,33 +155,6 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
124155
return NSMutableArray(array: self.allObjects)
125156
}
126157

127-
public convenience init(object anObject: Any) {
128-
self.init(array: [anObject])
129-
}
130-
131-
public convenience init(array: [Any]) {
132-
self.init(array: array, copyItems: false)
133-
}
134-
135-
public convenience init(array: [Any], copyItems: Bool) {
136-
137-
let optionalArray : [AnyObject] =
138-
copyItems ?
139-
array.map { return __SwiftValue.store($0).copy() as! NSObject } :
140-
array.map { return __SwiftValue.store($0) }
141-
142-
// This would have been nice, but "initializer delegation cannot be nested in another expression"
143-
// optionalArray.withUnsafeBufferPointer { ptr in
144-
// self.init(objects: ptr.baseAddress, count: array.count)
145-
// }
146-
let cnt = array.count
147-
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: cnt)
148-
buffer.initialize(from: optionalArray, count: cnt)
149-
self.init(objects: buffer, count: cnt)
150-
buffer.deinitialize(count: cnt)
151-
buffer.deallocate()
152-
}
153-
154158
open override func isEqual(_ value: Any?) -> Bool {
155159
switch value {
156160
case let other as [Any]:

TestFoundation/TestNSArray.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ class TestNSArray : XCTestCase {
7979
XCTAssertEqual(array3, array5)
8080
XCTAssertEqual(array4, array5)
8181

82+
let mutArray = NSMutableArray(array: [1, 2, 3, 4])
83+
let array6 = NSArray(array: mutArray)
84+
XCTAssertEqual(array6, mutArray)
85+
mutArray[0] = 0
86+
XCTAssertNotEqual(array6, mutArray)
87+
88+
let array7 = NSMutableArray(array: mutArray)
89+
XCTAssertEqual(array7, mutArray)
90+
array7.removeObject(at: 3)
91+
XCTAssertNotEqual(array7.count, mutArray.count)
8292
}
8393

8494
func test_constructorWithCopyItems() {

0 commit comments

Comments
 (0)