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 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 */
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
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.filePathURL == true else {
let userInfo = [NSLocalizedDescriptionKey : "The folder at “\(url)” does not exist or is not a file URL.", // NSLocalizedString() not yet available

Choose a reason for hiding this comment

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

It seems to me that the error message also isn't quite correct about the first part. You aren't checking whether a folder exist, but wether the URL given contains a path. (But I am not sure exactly what the existing Foundation framework does/reports in this case.)

Copy link
Contributor

Choose a reason for hiding this comment

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

if the url is a file url then it checks to see if the url has a path, if it does not it claims there is no such file (which is kind-of true-ish)
then it will funnel to writeToFile in the case of a file url (which has it's own error cases)
else it claims the url is an unsupported scheme.

NSURLErrorKey : url.absoluteString ?? ""]
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
8 changes: 4 additions & 4 deletions Foundation/NSDecimalNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@


/*************** Exceptions ***********/
public let NSDecimalNumberExactnessException: String = "" // NSUnimplemented
public let NSDecimalNumberOverflowException: String = "" // NSUnimplemented
public let NSDecimalNumberUnderflowException: String = "" // NSUnimplemented
public let NSDecimalNumberDivideByZeroException: String = "" // NSUnimplemented
public let NSDecimalNumberExactnessException: String = "NSDecimalNumberExactnessException"
public let NSDecimalNumberOverflowException: String = "NSDecimalNumberOverflowException"
public let NSDecimalNumberUnderflowException: String = "NSDecimalNumberUnderflowException"
public let NSDecimalNumberDivideByZeroException: String = "NSDecimalNumberDivideByZeroException"
Copy link
Contributor

Choose a reason for hiding this comment

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

These are not exactly part of NSData's url writing, it would be good to split them out to their own commits

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This and the one below NSRunLoop one are from previously a merged commit (cdde78c) from different contributor. There's one other commit from someone else that got attached to this when I pushed, but that has since been merged as well (65cdfac). Should I just close this and resubmit so it's not as messy?


/*************** Rounding and Exception behavior ***********/

Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSPort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public typealias NSSocketNativeHandle = Int32

public let NSPortDidBecomeInvalidNotification: String = "" // NSUnimplemented
public let NSPortDidBecomeInvalidNotification: String = "NSPortDidBecomeInvalidNotification"

public class NSPort : NSObject, NSCopying, NSCoding {

Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSRunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
//


public let NSDefaultRunLoopMode: String = "" // NSUnimplemented
public let NSRunLoopCommonModes: String = "" // NSUnimplemented
public let NSDefaultRunLoopMode: String = "NSDefaultRunLoopMode"
public let NSRunLoopCommonModes: String = "NSRunLoopCommonModes"
Copy link
Contributor

Choose a reason for hiding this comment

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

These probably will need to be the same as their CF counterparts when run loops will be supported.


public class NSRunLoop : NSObject {

Expand Down
30 changes: 15 additions & 15 deletions Foundation/NSURLProtectionSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,92 +12,92 @@
@const NSURLProtectionSpaceHTTP
@abstract The protocol for HTTP
*/
public let NSURLProtectionSpaceHTTP: String = "" // NSUnimplemented
public let NSURLProtectionSpaceHTTP: String = "NSURLProtectionSpaceHTTP"

/*!
@const NSURLProtectionSpaceHTTPS
@abstract The protocol for HTTPS
*/
public let NSURLProtectionSpaceHTTPS: String = "" // NSUnimplemented
public let NSURLProtectionSpaceHTTPS: String = "NSURLProtectionSpaceHTTPS"

/*!
@const NSURLProtectionSpaceFTP
@abstract The protocol for FTP
*/
public let NSURLProtectionSpaceFTP: String = "" // NSUnimplemented
public let NSURLProtectionSpaceFTP: String = "NSURLProtectionSpaceFTP"

/*!
@const NSURLProtectionSpaceHTTPProxy
@abstract The proxy type for http proxies
*/
public let NSURLProtectionSpaceHTTPProxy: String = "" // NSUnimplemented
public let NSURLProtectionSpaceHTTPProxy: String = "NSURLProtectionSpaceHTTPProxy"

/*!
@const NSURLProtectionSpaceHTTPSProxy
@abstract The proxy type for https proxies
*/
public let NSURLProtectionSpaceHTTPSProxy: String = "" // NSUnimplemented
public let NSURLProtectionSpaceHTTPSProxy: String = "NSURLProtectionSpaceHTTPSProxy"

/*!
@const NSURLProtectionSpaceFTPProxy
@abstract The proxy type for ftp proxies
*/
public let NSURLProtectionSpaceFTPProxy: String = "" // NSUnimplemented
public let NSURLProtectionSpaceFTPProxy: String = "NSURLProtectionSpaceFTPProxy"

/*!
@const NSURLProtectionSpaceSOCKSProxy
@abstract The proxy type for SOCKS proxies
*/
public let NSURLProtectionSpaceSOCKSProxy: String = "" // NSUnimplemented
public let NSURLProtectionSpaceSOCKSProxy: String = "NSURLProtectionSpaceSOCKSProxy"

/*!
@const NSURLAuthenticationMethodDefault
@abstract The default authentication method for a protocol
*/
public let NSURLAuthenticationMethodDefault: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodDefault: String = "NSURLAuthenticationMethodDefault"

/*!
@const NSURLAuthenticationMethodHTTPBasic
@abstract HTTP basic authentication. Equivalent to
NSURLAuthenticationMethodDefault for http.
*/
public let NSURLAuthenticationMethodHTTPBasic: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodHTTPBasic: String = "NSURLAuthenticationMethodHTTPBasic"

/*!
@const NSURLAuthenticationMethodHTTPDigest
@abstract HTTP digest authentication.
*/
public let NSURLAuthenticationMethodHTTPDigest: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodHTTPDigest: String = "NSURLAuthenticationMethodHTTPDigest"

/*!
@const NSURLAuthenticationMethodHTMLForm
@abstract HTML form authentication. Applies to any protocol.
*/
public let NSURLAuthenticationMethodHTMLForm: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodHTMLForm: String = "NSURLAuthenticationMethodHTMLForm"

/*!
@const NSURLAuthenticationMethodNTLM
@abstract NTLM authentication.
*/
public let NSURLAuthenticationMethodNTLM: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodNTLM: String = "NSURLAuthenticationMethodNTLM"

/*!
@const NSURLAuthenticationMethodNegotiate
@abstract Negotiate authentication.
*/
public let NSURLAuthenticationMethodNegotiate: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodNegotiate: String = "NSURLAuthenticationMethodNegotiate"

/*!
@const NSURLAuthenticationMethodClientCertificate
@abstract SSL Client certificate. Applies to any protocol.
*/
public let NSURLAuthenticationMethodClientCertificate: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodClientCertificate: String = "NSURLAuthenticationMethodClientCertificate"

/*!
@const NSURLAuthenticationMethodServerTrust
@abstract SecTrustRef validation required. Applies to any protocol.
*/
public let NSURLAuthenticationMethodServerTrust: String = "" // NSUnimplemented
public let NSURLAuthenticationMethodServerTrust: String = "NSURLAuthenticationMethodServerTrust"


/*!
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
//


public let NSGlobalDomain: String = "" // NSUnimplemented()
public let NSArgumentDomain: String = "" // NSUnimplemented()
public let NSRegistrationDomain: String = "" // NSUnimplemented()
public let NSGlobalDomain: String = "NSGlobalDomain"
public let NSArgumentDomain: String = "NSArgumentDomain"
public let NSRegistrationDomain: String = "NSRegistrationDomain"

public class NSUserDefaults : NSObject {

Expand Down Expand Up @@ -63,5 +63,5 @@ public class NSUserDefaults : NSObject {
public func objectIsForcedForKey(key: String, inDomain domain: String) -> Bool { NSUnimplemented() }
}

public let NSUserDefaultsDidChangeNotification: String = "" // NSUnimplemented()
public let NSUserDefaultsDidChangeNotification: String = "NSUserDefaultsDidChangeNotification"

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)
}
}
}










2 changes: 1 addition & 1 deletion TestFoundation/TestNSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class TestNSFileManger : XCTestCase {
XCTAssertEqual(fileType!, NSFileTypeRegular)

let fileOwnerAccountID = attrs[NSFileOwnerAccountID] as? NSNumber
XCTAssertNotEqual(fileOwnerAccountID!.longLongValue, 0)
XCTAssertNotNil(fileOwnerAccountID)

} catch let err {
XCTFail("\(err)")
Expand Down
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()
])