Skip to content

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

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
Dec 8, 2015
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
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 */
366C227D1C1649590069EBFD /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366C227C1C1649590069EBFD /* TestNSData.swift */; };
4DC1D0801C12EEEF00B5948A /* TestNSPipe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */; };
525AECED1BF2C9C500D15BB0 /* TestNSFileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */; };
528776141BF2629700CB0090 /* FoundationErrors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522C253A1BF16E1600804FC6 /* FoundationErrors.swift */; };
Expand Down Expand Up @@ -314,6 +315,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
366C227C1C1649590069EBFD /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSPipe.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>"; };
Expand Down Expand Up @@ -1015,6 +1017,7 @@
5BC1D8BC1BF3ADFE009D3973 /* TestNSCharacterSet.swift */,
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */,
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
366C227C1C1649590069EBFD /* TestNSData.swift */,
);
name = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1704,6 +1707,7 @@
4DC1D0801C12EEEF00B5948A /* TestNSPipe.swift in Sources */,
EA66F64E1BF1619600136161 /* TestNSIndexSet.swift in Sources */,
EA66F6541BF1619600136161 /* TestNSSet.swift in Sources */,
366C227D1C1649590069EBFD /* TestNSData.swift in Sources */,
EA66F64A1BF1619600136161 /* TestNSArray.swift in Sources */,
5BC1D8BE1BF3B09E009D3973 /* TestNSCharacterSet.swift in Sources */,
EA66F6561BF1619600136161 /* TestNSString.swift in Sources */,
Expand Down
17 changes: 15 additions & 2 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,22 @@ 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()
guard let path = url.path where url.fileURL == true else {
let userInfo = [NSLocalizedDescriptionKey : "The folder at “\(url)” does not exist or is not a file URL." as NSString, // NSLocalizedString() not yet available
NSURLErrorKey : url.absoluteString as NSString? ?? ""]
throw NSError(domain: NSCocoaErrorDomain, code: 4, userInfo: userInfo)
}
try writeToFile(path, options: writeOptionsMask)
}

internal func enumerateByteRangesUsingBlockRethrows(block: (UnsafePointer<Void>, NSRange, UnsafeMutablePointer<Bool>) throws -> Void) throws {
Expand Down
42 changes: 42 additions & 0 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 @@ -34,4 +34,5 @@ XCTMain([
TestNSFileManger(),
TestNSRange(),
TestNSXMLParser(),
TestNSData()
])