Skip to content

Commit 1c1aaf1

Browse files
committed
Merge branch 'pr/67'
2 parents f749c87 + a27b95b commit 1c1aaf1

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,7 @@
17331733
22B9C1E11C165D7A00DECFF9 /* TestNSDate.swift in Sources */,
17341734
848A30581C137B3500C83206 /* TestNSHTTPCookie.swift in Sources */,
17351735
EA66F6541BF1619600136161 /* TestNSSet.swift in Sources */,
1736+
366C227D1C1649590069EBFD /* TestNSData.swift in Sources */,
17361737
EA66F64A1BF1619600136161 /* TestNSArray.swift in Sources */,
17371738
5BC1D8BE1BF3B09E009D3973 /* TestNSCharacterSet.swift in Sources */,
17381739
EA66F6561BF1619600136161 /* TestNSString.swift in Sources */,

Foundation/NSData.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,22 @@ extension NSData {
458458
}
459459
return false
460460
}
461-
461+
462+
/// Write the contents of the receiver to a location specified by the given file URL.
463+
///
464+
/// - parameter url: The location to which the receiver’s contents will be written.
465+
/// - parameter writeOptionsMask: An option set specifying file writing options.
466+
///
467+
/// - throws: This method returns Void and is marked with the `throws` keyword to indicate that it throws an error in the event of failure.
468+
///
469+
/// 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).
462470
public func writeToURL(url: NSURL, options writeOptionsMask: NSDataWritingOptions) throws {
463-
NSUnimplemented()
471+
guard let path = url.path where url.fileURL == true else {
472+
let userInfo = [NSLocalizedDescriptionKey : "The folder at “\(url)” does not exist or is not a file URL.", // NSLocalizedString() not yet available
473+
NSURLErrorKey : url.absoluteString ?? ""] as Dictionary<String, Any>
474+
throw NSError(domain: NSCocoaErrorDomain, code: 4, userInfo: userInfo)
475+
}
476+
try writeToFile(path, options: writeOptionsMask)
464477
}
465478

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

TestFoundation/TestNSData.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,23 @@ class TestNSData: XCTestCase {
2626
("test_longDebugDescription", test_longDebugDescription),
2727
("test_limitDebugDescription", test_limitDebugDescription),
2828
("test_edgeDebugDescription", test_edgeDebugDescription),
29+
("test_writeToURLOptions", test_writeToURLOptions),
2930
]
3031
}
3132

33+
func test_writeToURLOptions() {
34+
let saveData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("Test", withExtension: "plist")!)
35+
let savePath = "/var/tmp/Test.plist"
36+
do {
37+
try saveData!.writeToFile(savePath, options: NSDataWritingOptions.DataWritingAtomic)
38+
let fileManager = NSFileManager.defaultManager()
39+
XCTAssertTrue(fileManager.fileExistsAtPath(savePath))
40+
try! fileManager.removeItemAtPath(savePath)
41+
} catch let error {
42+
XCTFail((error as! NSError).localizedDescription)
43+
}
44+
}
45+
3246
func test_emptyDescription() {
3347
let expected = "<>"
3448

0 commit comments

Comments
 (0)