Skip to content

Commit 48f6012

Browse files
committed
Implement -[NSData writeToURL:options:error:]
1 parent 58cfe37 commit 48f6012

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
366C227D1C1649590069EBFD /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366C227C1C1649590069EBFD /* TestNSData.swift */; };
1011
4DC1D0801C12EEEF00B5948A /* TestNSPipe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */; };
1112
525AECED1BF2C9C500D15BB0 /* TestNSFileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */; };
1213
528776141BF2629700CB0090 /* FoundationErrors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522C253A1BF16E1600804FC6 /* FoundationErrors.swift */; };
@@ -314,6 +315,7 @@
314315
/* End PBXCopyFilesBuildPhase section */
315316

316317
/* Begin PBXFileReference section */
318+
366C227C1C1649590069EBFD /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
317319
4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSPipe.swift; sourceTree = "<group>"; };
318320
522C253A1BF16E1600804FC6 /* FoundationErrors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationErrors.swift; sourceTree = "<group>"; };
319321
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSFileManager.swift; sourceTree = "<group>"; };
@@ -1015,6 +1017,7 @@
10151017
5BC1D8BC1BF3ADFE009D3973 /* TestNSCharacterSet.swift */,
10161018
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */,
10171019
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
1020+
366C227C1C1649590069EBFD /* TestNSData.swift */,
10181021
);
10191022
name = Tests;
10201023
sourceTree = "<group>";
@@ -1704,6 +1707,7 @@
17041707
4DC1D0801C12EEEF00B5948A /* TestNSPipe.swift in Sources */,
17051708
EA66F64E1BF1619600136161 /* TestNSIndexSet.swift in Sources */,
17061709
EA66F6541BF1619600136161 /* TestNSSet.swift in Sources */,
1710+
366C227D1C1649590069EBFD /* TestNSData.swift in Sources */,
17071711
EA66F64A1BF1619600136161 /* TestNSArray.swift in Sources */,
17081712
5BC1D8BE1BF3B09E009D3973 /* TestNSCharacterSet.swift in Sources */,
17091713
EA66F6561BF1619600136161 /* TestNSString.swift in Sources */,

Foundation/NSData.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,22 @@ extension NSData {
431431
}
432432
return false
433433
}
434-
434+
435+
/// Write the contents of the receiver to a location specified by the given file URL.
436+
///
437+
/// - parameter url: The location to which the receiver’s contents will be written.
438+
/// - parameter writeOptionsMask: An option set specifying file writing options.
439+
///
440+
/// - throws: This method returns Void and is marked with the `throws` keyword to indicate that it throws an error in the event of failure.
441+
///
442+
/// 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).
435443
public func writeToURL(url: NSURL, options writeOptionsMask: NSDataWritingOptions) throws {
436-
NSUnimplemented()
444+
guard let path = url.path where url.filePathURL == true else {
445+
let userInfo = [NSLocalizedDescriptionKey : "The folder at “\(url)” does not exist or is not a file URL.", // NSLocalizedString() not yet available
446+
NSURLErrorKey : url.absoluteString ?? ""]
447+
throw NSError(domain: NSCocoaErrorDomain, code: 4, userInfo: userInfo)
448+
}
449+
try writeToFile(path, options: writeOptionsMask)
437450
}
438451

439452
internal func enumerateByteRangesUsingBlockRethrows(block: (UnsafePointer<Void>, NSRange, UnsafeMutablePointer<Bool>) throws -> Void) throws {

TestFoundation/TestNSData.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
12+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
13+
import Foundation
14+
import XCTest
15+
#else
16+
import SwiftFoundation
17+
import SwiftXCTest
18+
#endif
19+
20+
21+
22+
class TestNSData : XCTestCase {
23+
24+
var allTests : [(String, () -> ())] {
25+
return [
26+
("test_writeToURLOptions", test_writeToURLOptions)
27+
]
28+
}
29+
30+
func test_writeToURLOptions() {
31+
let saveData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("Test", withExtension: "plist")!)
32+
let savePath = "/var/tmp/Test.plist"
33+
do {
34+
try saveData!.writeToFile(savePath, options: NSDataWritingOptions.DataWritingAtomic)
35+
let fileManager = NSFileManager.defaultManager()
36+
XCTAssertTrue(fileManager.fileExistsAtPath(savePath))
37+
try! fileManager.removeItemAtPath(savePath)
38+
} catch let error {
39+
XCTFail((error as! NSError).localizedDescription)
40+
}
41+
}
42+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ XCTMain([
3434
TestNSFileManger(),
3535
TestNSRange(),
3636
TestNSXMLParser(),
37+
TestNSData()
3738
])

0 commit comments

Comments
 (0)