Skip to content
Open
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
Use functions instead of operators for NSNumber comparisons
This is required for compatibility with Swift Foundation
on Linux and Android platforms. On those platforms, NSNumber
already implements comparison operators, which causes conflicts.
  • Loading branch information
aesilevich committed Aug 14, 2025
commit 3594d05dfc1e55fe9319e5c08ee9a5b23b81d117
22 changes: 11 additions & 11 deletions Source/SwiftyJSON/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ extension JSON: Swift.Comparable {}
public func == (lhs: JSON, rhs: JSON) -> Bool {

switch (lhs.type, rhs.type) {
case (.number, .number): return lhs.rawNumber == rhs.rawNumber
case (.number, .number): return eq(lhs.rawNumber, rhs.rawNumber)
case (.string, .string): return lhs.rawString == rhs.rawString
case (.bool, .bool): return lhs.rawBool == rhs.rawBool
case (.array, .array): return lhs.rawArray as NSArray == rhs.rawArray as NSArray
Expand All @@ -1173,7 +1173,7 @@ public func == (lhs: JSON, rhs: JSON) -> Bool {
public func <= (lhs: JSON, rhs: JSON) -> Bool {

switch (lhs.type, rhs.type) {
case (.number, .number): return lhs.rawNumber <= rhs.rawNumber
case (.number, .number): return le(lhs.rawNumber, rhs.rawNumber)
case (.string, .string): return lhs.rawString <= rhs.rawString
case (.bool, .bool): return lhs.rawBool == rhs.rawBool
case (.array, .array): return lhs.rawArray as NSArray == rhs.rawArray as NSArray
Expand All @@ -1186,7 +1186,7 @@ public func <= (lhs: JSON, rhs: JSON) -> Bool {
public func >= (lhs: JSON, rhs: JSON) -> Bool {

switch (lhs.type, rhs.type) {
case (.number, .number): return lhs.rawNumber >= rhs.rawNumber
case (.number, .number): return ge(lhs.rawNumber, rhs.rawNumber)
case (.string, .string): return lhs.rawString >= rhs.rawString
case (.bool, .bool): return lhs.rawBool == rhs.rawBool
case (.array, .array): return lhs.rawArray as NSArray == rhs.rawArray as NSArray
Expand All @@ -1199,7 +1199,7 @@ public func >= (lhs: JSON, rhs: JSON) -> Bool {
public func > (lhs: JSON, rhs: JSON) -> Bool {

switch (lhs.type, rhs.type) {
case (.number, .number): return lhs.rawNumber > rhs.rawNumber
case (.number, .number): return gt(lhs.rawNumber, rhs.rawNumber)
case (.string, .string): return lhs.rawString > rhs.rawString
default: return false
}
Expand All @@ -1208,7 +1208,7 @@ public func > (lhs: JSON, rhs: JSON) -> Bool {
public func < (lhs: JSON, rhs: JSON) -> Bool {

switch (lhs.type, rhs.type) {
case (.number, .number): return lhs.rawNumber < rhs.rawNumber
case (.number, .number): return lt(lhs.rawNumber, rhs.rawNumber)
case (.string, .string): return lhs.rawString < rhs.rawString
default: return false
}
Expand All @@ -1232,19 +1232,19 @@ extension NSNumber {
}
}

func == (lhs: NSNumber, rhs: NSNumber) -> Bool {
func eq(_ lhs: NSNumber, _ rhs: NSNumber) -> Bool {
switch (lhs.isBool, rhs.isBool) {
case (false, true): return false
case (true, false): return false
default: return lhs.compare(rhs) == .orderedSame
}
}

func != (lhs: NSNumber, rhs: NSNumber) -> Bool {
func neq(_ lhs: NSNumber, _ rhs: NSNumber) -> Bool {
return !(lhs == rhs)
}

func < (lhs: NSNumber, rhs: NSNumber) -> Bool {
func lt(_ lhs: NSNumber, _ rhs: NSNumber) -> Bool {

switch (lhs.isBool, rhs.isBool) {
case (false, true): return false
Expand All @@ -1253,7 +1253,7 @@ func < (lhs: NSNumber, rhs: NSNumber) -> Bool {
}
}

func > (lhs: NSNumber, rhs: NSNumber) -> Bool {
func gt(_ lhs: NSNumber, _ rhs: NSNumber) -> Bool {

switch (lhs.isBool, rhs.isBool) {
case (false, true): return false
Expand All @@ -1262,7 +1262,7 @@ func > (lhs: NSNumber, rhs: NSNumber) -> Bool {
}
}

func <= (lhs: NSNumber, rhs: NSNumber) -> Bool {
func le(_ lhs: NSNumber, _ rhs: NSNumber) -> Bool {

switch (lhs.isBool, rhs.isBool) {
case (false, true): return false
Expand All @@ -1271,7 +1271,7 @@ func <= (lhs: NSNumber, rhs: NSNumber) -> Bool {
}
}

func >= (lhs: NSNumber, rhs: NSNumber) -> Bool {
func ge(_ lhs: NSNumber, _ rhs: NSNumber) -> Bool {

switch (lhs.isBool, rhs.isBool) {
case (false, true): return false
Expand Down