Skip to content

Commit cc5704d

Browse files
authored
Remove URL in favor of swift-foundation's URL (swiftlang#4955)
* Remove URL in favor of swift-foundation's URL * Update swift-foundation dependency hash
1 parent 6e5d962 commit cc5704d

18 files changed

+122
-1098
lines changed

Diff for: Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ let package = Package(
7878
from: "0.0.5"),
7979
.package(
8080
url: "https://github.com/apple/swift-foundation",
81-
revision: "7eb8b598ad8f77ac743b2decc97d56bf350aedee"
81+
revision: "e991656bd02af48530811f1871b3351961b75d29"
8282
),
8383
],
8484
targets: [

Diff for: Sources/Foundation/FileManager.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ open class FileManager : NSObject {
972972
isDirectory.boolValue {
973973
for language in _preferredLanguages {
974974
let stringsFile = dotLocalized.appendingPathComponent(language).appendingPathExtension("strings")
975-
if let data = try? Data(contentsOf: stringsFile.path),
975+
if let data = try? Data(contentsOf: stringsFile),
976976
let plist = (try? PropertyListSerialization.propertyList(from: data, format: nil)) as? NSDictionary {
977977

978978
let localizedName = (plist[nameWithoutExtension] as? NSString)?._swiftObject
@@ -1034,13 +1034,13 @@ open class FileManager : NSObject {
10341034
/* 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.
10351035
*/
10361036
open func contents(atPath path: String) -> Data? {
1037-
return try? Data(contentsOf: path)
1037+
return try? Data(contentsOf: URL(fileURLWithPath: path))
10381038
}
10391039

10401040
@discardableResult
10411041
open func createFile(atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey : Any]? = nil) -> Bool {
10421042
do {
1043-
try (data ?? Data()).write(to: path, options: .atomic)
1043+
try (data ?? Data()).write(to: URL(fileURLWithPath: path), options: .atomic)
10441044
if let attr = attr {
10451045
try self.setAttributes(attr, ofItemAtPath: path)
10461046
}

Diff for: Sources/Foundation/NSArray.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
458458
open func write(to url: URL, atomically: Bool) -> Bool {
459459
do {
460460
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
461-
try pListData.write(to: url.path, options: atomically ? .atomic : [])
461+
try pListData.write(to: url, options: atomically ? .atomic : [])
462462
return true
463463
} catch {
464464
return false

Diff for: Sources/Foundation/NSCharacterSet.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
186186

187187
public convenience init?(contentsOfFile fName: String) {
188188
do {
189-
let data = try Data(contentsOf: fName)
189+
let data = try Data(contentsOf: URL(fileURLWithPath: fName))
190190
self.init(bitmapRepresentation: data)
191191
} catch {
192192
return nil

Diff for: Sources/Foundation/NSData.swift

-13
Original file line numberDiff line numberDiff line change
@@ -1202,19 +1202,6 @@ extension NSData {
12021202
}
12031203
}
12041204

1205-
// MARK: - Temporary URL support
1206-
1207-
extension Data {
1208-
// Temporary until SwiftFoundation supports this
1209-
public init(contentsOf url: URL, options: ReadingOptions = []) throws {
1210-
self = try .init(contentsOf: url.path, options: options)
1211-
}
1212-
1213-
public func write(to url: URL, options: WritingOptions = []) throws {
1214-
try write(to: url.path, options: options)
1215-
}
1216-
}
1217-
12181205
// MARK: - Bridging
12191206

12201207
extension Data {

Diff for: Sources/Foundation/NSDictionary.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
8282
@available(*, deprecated)
8383
public convenience init?(contentsOf url: URL) {
8484
do {
85-
guard let plistDoc = try? Data(contentsOf: url.path) else { return nil }
85+
guard let plistDoc = try? Data(contentsOf: url) else { return nil }
8686
let plistDict = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Dictionary<AnyHashable,Any>
8787
guard let plistDictionary = plistDict else { return nil }
8888
self.init(dictionary: plistDictionary)
@@ -509,7 +509,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
509509
open func write(to url: URL, atomically: Bool) -> Bool {
510510
do {
511511
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
512-
try pListData.write(to: url.path, options: atomically ? .atomic : [])
512+
try pListData.write(to: url, options: atomically ? .atomic : [])
513513
return true
514514
} catch {
515515
return false

Diff for: Sources/Foundation/NSError.swift

+21-11
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,6 @@ extension CocoaError : _BridgedStoredNSError {
605605
}
606606
}
607607

608-
extension CocoaError {
609-
// Temporary extension to take Foundation.URL, until FoundationEssentials.URL is fully ported
610-
public static func error(_ code: CocoaError.Code, userInfo: [String : AnyHashable]? = nil, url: Foundation.URL? = nil) -> Error {
611-
var info: [String : AnyHashable] = userInfo ?? [:]
612-
if let url = url {
613-
info[NSURLErrorKey] = url
614-
}
615-
return CocoaError(code, userInfo: info)
616-
}
617-
}
618-
619608
extension CocoaError.Code : _ErrorCodeProtocol {
620609
public typealias _ErrorType = CocoaError
621610
}
@@ -710,6 +699,27 @@ extension CocoaError {
710699
}
711700
}
712701

702+
extension CocoaError: _ObjectiveCBridgeable {
703+
public func _bridgeToObjectiveC() -> NSError {
704+
return self._nsError
705+
}
706+
707+
public static func _forceBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) {
708+
result = _unconditionallyBridgeFromObjectiveC(x)
709+
}
710+
711+
public static func _conditionallyBridgeFromObjectiveC(_ x: NSError, result: inout CocoaError?) -> Bool {
712+
result = CocoaError(_nsError: x)
713+
return true
714+
}
715+
716+
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSError?) -> CocoaError {
717+
var result: CocoaError?
718+
_forceBridgeFromObjectiveC(source!, result: &result)
719+
return result!
720+
}
721+
}
722+
713723
/// Describes errors in the URL error domain.
714724
public struct URLError : _BridgedStoredNSError {
715725
public let _nsError: NSError

Diff for: Sources/Foundation/NSString.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -1259,8 +1259,7 @@ extension NSString {
12591259
internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws {
12601260
var data = Data()
12611261
try _getExternalRepresentation(&data, url, enc)
1262-
// TODO: Use URL version when that is ready
1263-
try data.write(to: url.path, options: useAuxiliaryFile ? .atomic : [])
1262+
try data.write(to: url, options: useAuxiliaryFile ? .atomic : [])
12641263
}
12651264

12661265
open func write(to url: URL, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws {

Diff for: Sources/Foundation/NSURL.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ extension NSURL {
10241024

10251025
extension NSURL: _SwiftBridgeable {
10261026
typealias SwiftType = URL
1027-
internal var _swiftObject: SwiftType { return URL(reference: self) }
1027+
internal var _swiftObject: SwiftType { return self as URL }
10281028
}
10291029

10301030
extension CFURL : _NSBridgeable, _SwiftBridgeable {
@@ -1037,7 +1037,7 @@ extension CFURL : _NSBridgeable, _SwiftBridgeable {
10371037
extension URL : _NSBridgeable {
10381038
typealias NSType = NSURL
10391039
typealias CFType = CFURL
1040-
internal var _nsObject: NSType { return self.reference }
1040+
internal var _nsObject: NSType { return self as NSURL }
10411041
internal var _cfObject: CFType { return _nsObject._cfObject }
10421042
}
10431043

Diff for: Sources/Foundation/NSURLComponents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ open class NSURLComponents: NSObject, NSCopying {
8282
// 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.
8383
open var url: URL? {
8484
guard let result = _CFURLComponentsCopyURL(_components) else { return nil }
85-
return unsafeBitCast(result, to: URL.self)
85+
return result._swiftObject
8686
}
8787

8888
// 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.

0 commit comments

Comments
 (0)