Skip to content
Merged
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
24 changes: 22 additions & 2 deletions Sources/FoundationEssentials/FileManager/FileManager+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ func _readFileAttributePrimitive(_ value: Any?, as type: Bool.Type) -> Bool? {
return nil
}

#if !FOUNDATION_FRAMEWORK
@_spi(SwiftCorelibsFoundation)
public protocol _NSNumberInitializer {
static func initialize(value: Bool) -> Any
static func initialize(value: some BinaryInteger) -> Any
}

private let _nsNumberInitializer: (any _NSNumberInitializer.Type)? = {
_typeByName("Foundation._FoundationNSNumberInitializer") as? any _NSNumberInitializer.Type
}()
#endif

func _writeFileAttributePrimitive<T: BinaryInteger, U: BinaryInteger>(_ value: T, as type: U.Type) -> Any {
#if FOUNDATION_FRAMEWORK
if let int = Int64(exactly: value) {
Expand All @@ -114,15 +126,23 @@ func _writeFileAttributePrimitive<T: BinaryInteger, U: BinaryInteger>(_ value: T
NSNumber(value: UInt64(value))
}
#else
U(value)
if let ns = _nsNumberInitializer?.initialize(value: value) {
return ns
} else {
return U(value)
}
#endif
}

func _writeFileAttributePrimitive(_ value: Bool) -> Any {
#if FOUNDATION_FRAMEWORK
NSNumber(value: value)
#else
value
if let ns = _nsNumberInitializer?.initialize(value: value) {
return ns
} else {
return value
}
#endif
}

Expand Down