Skip to content

Remove URL in favor of swift-foundation's URL #4955

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 2 commits into from
May 14, 2024
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let package = Package(
from: "0.0.5"),
.package(
url: "https://github.com/apple/swift-foundation",
revision: "7eb8b598ad8f77ac743b2decc97d56bf350aedee"
revision: "e991656bd02af48530811f1871b3351961b75d29"
),
],
targets: [
Expand Down
6 changes: 3 additions & 3 deletions Sources/Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ open class FileManager : NSObject {
isDirectory.boolValue {
for language in _preferredLanguages {
let stringsFile = dotLocalized.appendingPathComponent(language).appendingPathExtension("strings")
if let data = try? Data(contentsOf: stringsFile.path),
if let data = try? Data(contentsOf: stringsFile),
let plist = (try? PropertyListSerialization.propertyList(from: data, format: nil)) as? NSDictionary {

let localizedName = (plist[nameWithoutExtension] as? NSString)?._swiftObject
Expand Down Expand Up @@ -1034,13 +1034,13 @@ open class FileManager : NSObject {
/* These methods are provided here for compatibility. The corresponding methods on NSData which return NSErrors should be regarded as the primary method of creating a file from an NSData or retrieving the contents of a file as an NSData.
*/
open func contents(atPath path: String) -> Data? {
return try? Data(contentsOf: path)
return try? Data(contentsOf: URL(fileURLWithPath: path))
}

@discardableResult
open func createFile(atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey : Any]? = nil) -> Bool {
do {
try (data ?? Data()).write(to: path, options: .atomic)
try (data ?? Data()).write(to: URL(fileURLWithPath: path), options: .atomic)
if let attr = attr {
try self.setAttributes(attr, ofItemAtPath: path)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
open func write(to url: URL, atomically: Bool) -> Bool {
do {
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
try pListData.write(to: url.path, options: atomically ? .atomic : [])
try pListData.write(to: url, options: atomically ? .atomic : [])
return true
} catch {
return false
Expand Down
2 changes: 1 addition & 1 deletion Sources/Foundation/NSCharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin

public convenience init?(contentsOfFile fName: String) {
do {
let data = try Data(contentsOf: fName)
let data = try Data(contentsOf: URL(fileURLWithPath: fName))
self.init(bitmapRepresentation: data)
} catch {
return nil
Expand Down
13 changes: 0 additions & 13 deletions Sources/Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1202,19 +1202,6 @@ extension NSData {
}
}

// MARK: - Temporary URL support

extension Data {
// Temporary until SwiftFoundation supports this
public init(contentsOf url: URL, options: ReadingOptions = []) throws {
self = try .init(contentsOf: url.path, options: options)
}

public func write(to url: URL, options: WritingOptions = []) throws {
try write(to: url.path, options: options)
}
}

// MARK: - Bridging

extension Data {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
@available(*, deprecated)
public convenience init?(contentsOf url: URL) {
do {
guard let plistDoc = try? Data(contentsOf: url.path) else { return nil }
guard let plistDoc = try? Data(contentsOf: url) else { return nil }
let plistDict = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Dictionary<AnyHashable,Any>
guard let plistDictionary = plistDict else { return nil }
self.init(dictionary: plistDictionary)
Expand Down Expand Up @@ -509,7 +509,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
open func write(to url: URL, atomically: Bool) -> Bool {
do {
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
try pListData.write(to: url.path, options: atomically ? .atomic : [])
try pListData.write(to: url, options: atomically ? .atomic : [])
return true
} catch {
return false
Expand Down
32 changes: 21 additions & 11 deletions Sources/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -605,17 +605,6 @@ extension CocoaError : _BridgedStoredNSError {
}
}

extension CocoaError {
// Temporary extension to take Foundation.URL, until FoundationEssentials.URL is fully ported
public static func error(_ code: CocoaError.Code, userInfo: [String : AnyHashable]? = nil, url: Foundation.URL? = nil) -> Error {
var info: [String : AnyHashable] = userInfo ?? [:]
if let url = url {
info[NSURLErrorKey] = url
}
return CocoaError(code, userInfo: info)
}
}

extension CocoaError.Code : _ErrorCodeProtocol {
public typealias _ErrorType = CocoaError
}
Expand Down Expand Up @@ -769,6 +758,27 @@ extension CocoaError {
}
}

extension CocoaError: _ObjectiveCBridgeable {
public func _bridgeToObjectiveC() -> NSError {
return self._nsError
}

public static func _forceBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) {
result = _unconditionallyBridgeFromObjectiveC(x)
}

public static func _conditionallyBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) -> Bool {
result = CocoaError(_nsError: x)
return true
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSError?) -> CocoaError {
var result: CocoaError?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
}

/// Describes errors in the URL error domain.
public struct URLError : _BridgedStoredNSError {
public let _nsError: NSError
Expand Down
3 changes: 1 addition & 2 deletions Sources/Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1259,8 +1259,7 @@ extension NSString {
internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws {
var data = Data()
try _getExternalRepresentation(&data, url, enc)
// TODO: Use URL version when that is ready
try data.write(to: url.path, options: useAuxiliaryFile ? .atomic : [])
try data.write(to: url, options: useAuxiliaryFile ? .atomic : [])
}

open func write(to url: URL, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ extension NSURL {

extension NSURL: _SwiftBridgeable {
typealias SwiftType = URL
internal var _swiftObject: SwiftType { return URL(reference: self) }
internal var _swiftObject: SwiftType { return self as URL }
}

extension CFURL : _NSBridgeable, _SwiftBridgeable {
Expand All @@ -1037,7 +1037,7 @@ extension CFURL : _NSBridgeable, _SwiftBridgeable {
extension URL : _NSBridgeable {
typealias NSType = NSURL
typealias CFType = CFURL
internal var _nsObject: NSType { return self.reference }
internal var _nsObject: NSType { return self as NSURL }
internal var _cfObject: CFType { return _nsObject._cfObject }
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Foundation/NSURLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ open class NSURLComponents: NSObject, NSCopying {
// Returns a URL created from the NSURLComponents. If the NSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
open var url: URL? {
guard let result = _CFURLComponentsCopyURL(_components) else { return nil }
return unsafeBitCast(result, to: URL.self)
return result._swiftObject
}

// Returns a URL created from the NSURLComponents relative to a base URL. If the NSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
Expand Down
Loading