Skip to content

Commit

Permalink
Add workaround for nil detection issue in Xcode 16
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jul 13, 2024
1 parent a468e32 commit 8b55b34
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/AnyExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,21 @@ extension AnyExpression {

// Test if a value is nil
static func isNil(_ value: Any) -> Bool {
if let optional = value as? _Optional {
switch value {
case let optional as _Optional:
guard let value = optional.value else {
return true
}
return isNil(value)
case is NSNull:
return true
case is _String:
// Avoid treating "nil" as nil
return false
default:
// Workaround for _Optional not working in Xcode 16
return String(describing: value) == "nil"
}
return value is NSNull
}

// Test if a value supports subscripting
Expand Down
15 changes: 15 additions & 0 deletions Tests/AnyExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,21 @@ class AnyExpressionTests: XCTestCase {
XCTAssertNil(try expression.evaluate() as String?)
}

func testStringNilNotTreatedAsNil() {
let expression = AnyExpression("'nil'")
XCTAssertNotNil(try expression.evaluate() as String?)
}

func testNSStringNilNotTreatedAsNil() {
let expression = AnyExpression("foo", constants: ["foo": "nil" as NSString])
XCTAssertNotNil(try expression.evaluate() as String?)
}

func testSubstringNilNotTreatedAsNil() {
let expression = AnyExpression("foo", constants: ["foo": Substring("nil")])
XCTAssertNotNil(try expression.evaluate() as String?)
}

// MARK: Errors

func testUnknownOperator() {
Expand Down

0 comments on commit 8b55b34

Please sign in to comment.