Skip to content

Fixes wrong behavior of NSArray.indexOfObject… #276

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 1 commit into from
Mar 21, 2016
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
8 changes: 4 additions & 4 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,15 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
}
}

guard searchForInsertionIndex && lastEqual else {
if !searchForInsertionIndex {
return result
}

guard result == NSNotFound else {
return result + 1
if result == NSNotFound {
return indexOfLeastGreaterThanObj
}

return indexOfLeastGreaterThanObj
return lastEqual ? result + 1 : result
}


Expand Down
8 changes: 8 additions & 0 deletions TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ class TestNSArray : XCTestCase {
let rangeLength = 13
let endOfArray = objectIndexInArray(array, value: 10, startingFrom: rangeStart, length: rangeLength, options: [.InsertionIndex, .LastEqual])
XCTAssertTrue(endOfArray == (rangeStart + rangeLength), "...or the index at the end of the array if the object is larger than all other elements.")

let arrayOfTwo = NSArray(array: [NSNumber(int: 0), NSNumber(int: 2)])
let indexInMiddle = objectIndexInArray(arrayOfTwo, value: 1, startingFrom: 0, length: 2, options: [.InsertionIndex, .FirstEqual])
XCTAssertEqual(indexInMiddle, 1, "If no match found item should be inserted before least greater object")
let indexInMiddle2 = objectIndexInArray(arrayOfTwo, value: 1, startingFrom: 0, length: 2, options: [.InsertionIndex, .LastEqual])
XCTAssertEqual(indexInMiddle2, 1, "If no match found item should be inserted before least greater object")
let indexInMiddle3 = objectIndexInArray(arrayOfTwo, value: 1, startingFrom: 0, length: 2, options: [.InsertionIndex])
XCTAssertEqual(indexInMiddle3, 1, "If no match found item should be inserted before least greater object")
}


Expand Down