Skip to content

Commit d66717d

Browse files
author
Sergey Minakov
committed
Change NSURL 'checkResourceIsReachableAndReturnError' to 'checkResourceIsReachable' with throw as Linux does not support Autoreleasing pointers
1 parent 086d130 commit d66717d

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

Foundation/NSURL.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -610,25 +610,23 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
610610
*/
611611
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
612612
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
613-
open func checkResourceIsReachableAndReturnError(_ error: UnsafeMutablePointer<NSError?>?) -> Bool {
613+
// TODO: should be `checkResourceIsReachableAndReturnError` with autoreleased error parameter.
614+
// Currently Autoreleased pointers is not supported on Linux.
615+
open func checkResourceIsReachable() throws -> Bool {
614616
guard isFileURL,
615617
let path = path else {
616-
if let error = error {
617-
error.pointee = NSError(domain: NSCocoaErrorDomain,
618-
code: CocoaError.Code.fileNoSuchFile.rawValue)
619-
}
620-
return false
618+
throw NSError(domain: NSCocoaErrorDomain,
619+
code: CocoaError.Code.fileNoSuchFile.rawValue)
620+
//return false
621621
}
622622

623623
guard FileManager.default.fileExists(atPath: path) else {
624-
if let error = error {
625-
error.pointee = NSError(domain: NSCocoaErrorDomain,
626-
code: CocoaError.Code.fileReadNoSuchFile.rawValue,
627-
userInfo: [
628-
"NSURL" : self,
629-
"NSFilePath" : path])
630-
}
631-
return false
624+
throw NSError(domain: NSCocoaErrorDomain,
625+
code: CocoaError.Code.fileReadNoSuchFile.rawValue,
626+
userInfo: [
627+
"NSURL" : self,
628+
"NSFilePath" : path])
629+
//return false
632630
}
633631

634632
return true

Foundation/URL.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -931,14 +931,7 @@ public struct URL : ReferenceConvertible, Equatable {
931931
///
932932
/// This method synchronously checks if the resource's backing store is reachable. Checking reachability is appropriate when making decisions that do not require other immediate operations on the resource, e.g. periodic maintenance of UI state that depends on the existence of a specific document. When performing operations such as opening a file or copying resource properties, it is more efficient to simply try the operation and handle failures. This method is currently applicable only to URLs for file system resources. For other URL types, `false` is returned.
933933
public func checkResourceIsReachable() throws -> Bool {
934-
var error: NSError?
935-
let result = _url.checkResourceIsReachableAndReturnError(&error)
936-
937-
guard error == nil else {
938-
throw error!
939-
}
940-
941-
return result
934+
return try _url.checkResourceIsReachable()
942935
}
943936

944937
// MARK: - Bridging Support

TestFoundation/TestNSURL.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,19 +450,30 @@ class TestNSURL : XCTestCase {
450450
XCTFail()
451451
}
452452

453-
var nsURL: NSURL? = NSURL(fileURLWithPath: "/usr")
454-
var error: NSError?
455-
XCTAssertEqual(true, nsURL?.checkResourceIsReachableAndReturnError(&error))
456-
XCTAssertNil(error)
453+
var nsURL = NSURL(fileURLWithPath: "/usr")
454+
XCTAssertEqual(true, try? nsURL.checkResourceIsReachable())
457455

458-
nsURL = NSURL(string: "https://www.swift.org")
459-
XCTAssertEqual(false, nsURL?.checkResourceIsReachableAndReturnError(&error))
460-
XCTAssertNotNil(error)
461-
error = nil
456+
nsURL = NSURL(string: "https://www.swift.org")!
457+
do {
458+
_ = try nsURL.checkResourceIsReachable()
459+
XCTFail()
460+
} catch let error as NSError {
461+
XCTAssertEqual(NSCocoaErrorDomain, error.domain)
462+
XCTAssertEqual(CocoaError.Code.fileNoSuchFile.rawValue, error.code)
463+
} catch {
464+
XCTFail()
465+
}
462466

463467
nsURL = NSURL(fileURLWithPath: "/some_random_path")
464-
XCTAssertEqual(false, nsURL?.checkResourceIsReachableAndReturnError(&error))
465-
XCTAssertNotNil(error)
468+
do {
469+
_ = try nsURL.checkResourceIsReachable()
470+
XCTFail()
471+
} catch let error as NSError {
472+
XCTAssertEqual(NSCocoaErrorDomain, error.domain)
473+
XCTAssertEqual(CocoaError.Code.fileReadNoSuchFile.rawValue, error.code)
474+
} catch {
475+
XCTFail()
476+
}
466477
}
467478

468479
func test_copy() {

0 commit comments

Comments
 (0)