Skip to content

Return file attributes as NSNumbers when SCL-F is imported #641

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
May 29, 2024
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