Skip to content

Commit 1741fc5

Browse files
authored
Merge pull request #1730 from philium/refactor-NSOrderedSet.objectsAtIndexes
2 parents c28f360 + 8a68a27 commit 1741fc5

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

Foundation/NSOrderedSet.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,18 @@ extension NSOrderedSet {
156156
}
157157
}
158158

159+
/// Returns an array with the objects at the specified indexes in the
160+
/// ordered set.
161+
///
162+
/// - Parameter indexes: The indexes.
163+
/// - Returns: An array of objects in the ascending order of their indexes
164+
/// in `indexes`.
165+
///
166+
/// - Complexity: O(*n*), where *n* is the number of indexes in `indexes`.
167+
/// - Precondition: The indexes in `indexes` are within the
168+
/// bounds of the ordered set.
159169
open func objects(at indexes: IndexSet) -> [Any] {
160-
var entries = [Any]()
161-
for idx in indexes {
162-
entries.append(object(at: idx))
163-
}
164-
return entries
170+
return indexes.map { object(at: $0) }
165171
}
166172

167173
public var firstObject: Any? {

TestFoundation/TestNSOrderedSet.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,8 @@ class TestNSOrderedSet : XCTestCase {
109109

110110
func test_ObjectsAtIndexes() {
111111
let set = NSOrderedSet(array: ["foo", "bar", "baz", "1", "2", "3"])
112-
var indexSet = IndexSet()
113-
indexSet.insert(1)
114-
indexSet.insert(3)
115-
indexSet.insert(5)
116-
let objects = set.objects(at: indexSet)
112+
let objects = set.objects(at: [1, 3, 5])
113+
XCTAssertEqual(objects.count, 3)
117114
XCTAssertEqual(objects[0] as? String, "bar")
118115
XCTAssertEqual(objects[1] as? String, "1")
119116
XCTAssertEqual(objects[2] as? String, "3")

0 commit comments

Comments
 (0)