Skip to content

Implement -[NSData writeToURL:options:error:] #57

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
364F8AF71C1544330058DC9F /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 364F8AF61C1544330058DC9F /* TestNSData.swift */; };
525AECED1BF2C9C500D15BB0 /* TestNSFileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */; };
528776141BF2629700CB0090 /* FoundationErrors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522C253A1BF16E1600804FC6 /* FoundationErrors.swift */; };
528776191BF27D9500CB0090 /* Test.plist in Resources */ = {isa = PBXBuildFile; fileRef = 528776181BF27D9500CB0090 /* Test.plist */; };
Expand Down Expand Up @@ -313,6 +314,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
364F8AF61C1544330058DC9F /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
522C253A1BF16E1600804FC6 /* FoundationErrors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationErrors.swift; sourceTree = "<group>"; };
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSFileManager.swift; sourceTree = "<group>"; };
528776181BF27D9500CB0090 /* Test.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Test.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1001,6 +1003,7 @@
isa = PBXGroup;
children = (
EA66F63C1BF1619600136161 /* TestNSArray.swift */,
364F8AF61C1544330058DC9F /* TestNSData.swift */,
EA66F63D1BF1619600136161 /* TestNSDictionary.swift */,
EA66F63E1BF1619600136161 /* TestNSIndexSet.swift */,
EA66F63F1BF1619600136161 /* TestNSNumber.swift */,
Expand Down Expand Up @@ -1696,6 +1699,7 @@
files = (
525AECED1BF2C9C500D15BB0 /* TestNSFileManager.swift in Sources */,
EA66F6501BF1619600136161 /* TestNSNumber.swift in Sources */,
364F8AF71C1544330058DC9F /* TestNSData.swift in Sources */,
E876A73E1C1180E000F279EC /* TestNSRange.swift in Sources */,
EA66F6521BF1619600136161 /* TestNSPropertyList.swift in Sources */,
EA66F64E1BF1619600136161 /* TestNSIndexSet.swift in Sources */,
Expand Down
22 changes: 20 additions & 2 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,27 @@ extension NSData {
}
return false
}


/// Write the contents of the receiver to a location specified by the given file URL.
///
/// - parameter url: The location to which the receiver’s contents will be written.
/// - parameter writeOptionsMask: An option set specifying file writing options.
///
/// - throws: This method returns Void and is marked with the `throws` keyword to indicate that it throws an error in the event of failure.
///
/// This method is invoked in a `try` expression and the caller is responsible for handling any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42) in [The Swift Programming Language](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html#//apple_ref/doc/uid/TP40014097) and [Error Handling](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID10) in [Using Swift with Cocoa and Objective-C](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216).
public func writeToURL(url: NSURL, options writeOptionsMask: NSDataWritingOptions) throws {
NSUnimplemented()
if let path = url.path {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also check if the url gives back true for isFileURL? So that it for example would fail (as expected) for the url http://www.apple.com/some/path/writable/by/the/user?

do {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need for do {} catch {} blocks, you can simply use try writeToFile(path, options: writeOptionsMask)

try writeToFile(path, options: writeOptionsMask)
} catch let error {
throw error
}
} else {
let userInfo = [NSLocalizedDescriptionKey : "The folder “\(url.path)” doesn’t exist.", // NSLocalizedString() not yet available
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url.path will be nil in string interpolation

NSURLErrorKey : url.absoluteString ?? ""]
throw NSError(domain: NSCocoaErrorDomain, code: 4, userInfo: userInfo)
}
}

internal func enumerateByteRangesUsingBlockRethrows(block: (UnsafePointer<Void>, NSRange, UnsafeMutablePointer<Bool>) throws -> Void) throws {
Expand Down
52 changes: 52 additions & 0 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//



#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif



class TestNSData : XCTestCase {

var allTests : [(String, () -> ())] {
return [
("test_writeToURLOptions", test_writeToURLOptions)
]
}

func test_writeToURLOptions() {
let saveData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("Test", withExtension: "plist")!)
let savePath = "/var/tmp/Test.plist"
do {
try saveData!.writeToFile(savePath, options: NSDataWritingOptions.DataWritingAtomic)
let fileManager = NSFileManager.defaultManager()
XCTAssertTrue(fileManager.fileExistsAtPath(savePath))
try! fileManager.removeItemAtPath(savePath)
} catch let error {
XCTFail((error as! NSError).localizedDescription)
}
}
}










1 change: 1 addition & 0 deletions TestFoundation/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ XCTMain([
TestNSFileManger(),
TestNSRange(),
TestNSXMLParser(),
TestNSData()
])