Is There a Writetofile Equivalent for Swift 3's 'Data' Type

Is there a writeToFile equivalent for Swift 3's 'Data' type?

Use write(to: fileURL).

For example:

let fileURL = try! FileManager.default
.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent("test.jpg")

do {
try jpegData.write(to: fileURL, options: .atomic)
} catch {
print(error)
}

Or, if you really are stuck with a path, convert that to a URL:

do {
try data.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch {
print(error)
}

But, generally, it's preferable to use URL references throughout your code nowadays, retiring the use of path strings.

What is the equivalent to `thing = this() || that` in Swift 3?

There's no exact equivalent in Swift, as:

  • No other types than Bool cannot be converted to Bool

  • You cannot write a function returning Bool or Int

(In most languages is a little bit exaggerated, you cannot write such thing in Java, C# or many other strongly-typed languages.)

The most similar thing in Swift is nil-coalescing operator -- ??.

Assume this() returns Int? (aka Optional<Int>), and that() returns Int (non-Optional):

func this() -> Int? {
//...
if someCondition() {
return intValue
} else {
return nil
}
}

func that() -> Int {
//...
return anotherIntValue
}

And use nil-coalescing operator like this:

let result = this() ?? that()

In this assignment, if this() returns non-nil value, then that() is not evaluated, and that non-nil value is assigned to result. When this() returns nil, that() is evaluated and the value is assigned to result.

What's the equivalent of NSArray's writeToFile: atomically: in Swift and Array?

I am not sure if the "native" ability for writing arrays to files exists in Swift, but since arrays can be bridged to NSArrays, you should be able to write your arrays to a file like this:

let mySwiftArray = ... // Your Swift array
let cocoaArray : NSArray = mySwiftArray
cocoaArray.writeToFile(filePath, atomically:true);

The second line creates a "Bridged" object of type NSArray, which should give you access to all methods from the foundation.

What is the equivalent to `thing = this() || that` in Swift 3?

There's no exact equivalent in Swift, as:

  • No other types than Bool cannot be converted to Bool

  • You cannot write a function returning Bool or Int

(In most languages is a little bit exaggerated, you cannot write such thing in Java, C# or many other strongly-typed languages.)

The most similar thing in Swift is nil-coalescing operator -- ??.

Assume this() returns Int? (aka Optional<Int>), and that() returns Int (non-Optional):

func this() -> Int? {
//...
if someCondition() {
return intValue
} else {
return nil
}
}

func that() -> Int {
//...
return anotherIntValue
}

And use nil-coalescing operator like this:

let result = this() ?? that()

In this assignment, if this() returns non-nil value, then that() is not evaluated, and that non-nil value is assigned to result. When this() returns nil, that() is evaluated and the value is assigned to result.

Value of type 'Data' has no member 'writeToURL'

atomically: false is equal to no options, you can omit the parameter.

So it's simply

do {
try data.write(to: manifestFileURL)
} catch let error as NSError {
self.didFailWithError(WebAppError.fileSystemFailure(reason: "Could not write asset manifest to: \(manifestFileURL)", underlyingError: error))
}

The catch clause handles the error.

Swift - Write an Array to a Text File

You need to reduce your array back down to a string:

var output = reduce(array, "") { (existing, toAppend) in
if existing.isEmpty {
return toAppend
}
else {
return "\(existing)\n\(toAppend)"
}
}
output.writeToFile(...)

The reduce method takes a collection and merges it all into a single instance. It takes an initial instance and closure to merge all elements of the collection into that original instance.

My example takes an empty string as its initial instance. The closure then checks if the existing output is empty. If it is, it only has to return the text to append, otherwise, it uses String Interpolation to return the existing output and the new element with a newline in between.

Using various syntactic sugar features from Swift, the whole reduction can be reduced to:

var output = reduce(array, "") { $0.isEmpty ? $1 : "\($0)\n\($1)" }


Related Topics



Leave a reply



Submit