Member-only story

@discardableResult Attribute in Swift

Get rid of that annoying warning

Steven Curtis
3 min readApr 22, 2020
Photo by dylan nolte on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

It’s one of those things. Swift gives you a warning and you can change a piece of code to suppress the warning.

You change it, and life goes on.

Then you discover that an attribute can help you out.

That’s a bingo.

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE) OR Coding in Swift Playgrounds (guide HERE)

Terminology

attribute: A way of providing information about a declaration of type

The case for the @discardableResult

In my Medium post about securing data in the keychain I had a rather nice function that returns the OSStatus after the save operation is complete.

Here is the function signature:

func save(key: String, data: Data) -> OSStatus

So to save a piece of data I decided that I wasn’t interested in the result of the function. This is because if I don’t Swift generates a warning that the variable is not used

--

--

No responses yet