Skip to content

Refactors NSOrderedSet.objects(at:) to use map(_:) #1730

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions Foundation/NSOrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,18 @@ extension NSOrderedSet {
}
}

/// Returns an array with the objects at the specified indexes in the
/// ordered set.
///
/// - Parameter indexes: The indexes.
/// - Returns: An array of objects in the ascending order of their indexes
/// in `indexes`.
///
/// - Complexity: O(*n*), where *n* is the number of indexes in `indexes`.
/// - Precondition: The indexes in `indexes` are within the
/// bounds of the ordered set.
open func objects(at indexes: IndexSet) -> [Any] {
var entries = [Any]()
for idx in indexes {
entries.append(object(at: idx))
}
return entries
return indexes.map { object(at: $0) }
}

public var firstObject: Any? {
Expand Down
7 changes: 2 additions & 5 deletions TestFoundation/TestNSOrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,8 @@ class TestNSOrderedSet : XCTestCase {

func test_ObjectsAtIndexes() {
let set = NSOrderedSet(array: ["foo", "bar", "baz", "1", "2", "3"])
var indexSet = IndexSet()
indexSet.insert(1)
indexSet.insert(3)
indexSet.insert(5)
let objects = set.objects(at: indexSet)
let objects = set.objects(at: [1, 3, 5])
XCTAssertEqual(objects.count, 3)
XCTAssertEqual(objects[0] as? String, "bar")
XCTAssertEqual(objects[1] as? String, "1")
XCTAssertEqual(objects[2] as? String, "3")
Expand Down