Give your simulator superpowers

RocketSim: An Essential Developer Tool
as recommended by Apple

@discardableResult in Swift explained: Ignoring return values

While writing methods in Swift you’re often running into scenarios in which you sometimes want to ignore the return value while in other cases you want to know the return value. The @discardableResult attribute allows us to enable both cases without having to deal with annoying warnings or underscore replacements.

It’s a small feature in Swift but it’s something you wish you knew once you get to know it.

When should you use the discardable result attribute?

It’s important to think through the decision to use the @discardableResult attribute. It’s easy to simply add the attribute so the following warning disappears:

Result of call to ‘updateName’ is unused

It can be quite an annoying warning that appears in many places throughout your project.

The warning that appears if you're not using the @discardableResult attribute
The warning that appears if you’re not using the @discardableResult attribute

However, it’s not always the right decision to simply add the @discardableResult attribute. In the above example, it’s better to force the user to handle the result value of the APIProvider method:

enum APIProvider {
    static func updateName(_ name: String) -> Result<User, Error> {
        // .. Handle API endpoint, example result:
        return .success(User(name: name))
    }
}

The result could also be an error in which case it’s important to tell the user that something went wrong. Therefore, decide for your methods whether it’s important to handle the result or not.

Stay updated with the latest in Swift

Join 20,010 Swift developers in our exclusive newsletter for the latest insights, tips, and updates. Don't miss out – join today!

You can always unsubscribe, no hard feelings.

How to use @discardableResult in Swift

The above example with the resulting warning could’ve been solved without the discardable result by using an underscore as a property name:

final class UpdateNameViewController {
    func didEnterName(_ name: String) {
        /// The underscore makes the warning go away.
        _ = APIProvider.updateName(name)
    }
}

However, having lots of underscore property names throughout your projects isn’t really clean too. Therefore, it’s better to use the @discardableResult keyword in front of your method definition:

enum APIProvider {
    @discardableResult static func updateName(_ name: String) -> Result<User, Error> {
        // .. Handle API endpoint, example result:
        return .success(User(name: name))
    }
}

The discardable result attribute allows you to use the return value if you want while you can decide as well to just ignore it. This keeps your code clean and removes any related warnings in your project.

Conclusion

The discardable result attribute might be less known but is really useful to hide warnings pointing to unused return values. Simply add the @discardableResult attribute to your method and the warnings disappear. Decide carefully for each method whether it’s important to handle the return value or not. In some cases, it might be better to force the user of your method to handle the result.

If you like to improve your Swift knowledge, even more, check out the Swift category page. Feel free to contact me or tweet to me on Twitter if you have any additional tips or feedback.

Thanks!

 
Antoine van der Lee

Written by

Antoine van der Lee

iOS Developer since 2010, former Staff iOS Engineer at WeTransfer and currently full-time Indie Developer & Founder at SwiftLee. Writing a new blog post every week related to Swift, iOS and Xcode. Regular speaker and workshop host.

Are you ready to

Turn your side projects into independence?

Learn my proven steps to transform your passion into profit.