The @discardableResult Attribute in Swift

func save(text: String, to file: String) -> Bool {
do {
guard let docDirURL =
FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask).first
else { return false }

let saveURL = docDirURL
.appendingPathComponent(file)
.appendingPathExtension("txt")

try text.write(to: saveURL,
atomically: true,
encoding: .utf8)
return true
}
catch {
return false
}
}
let message = "Hello World!"
let result = save(text: message, to: "myFile")
_ = save(text: message, to: "myFile")
@discardableResult
func save(text: String, to file: String) -> Bool {
...
}
save(text: message, to: "myFile")

An iOS & macOS app maker writing code in Swift. Author of countless programming tutorials. Content creator. https://serialcoder.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store