@discardableResult in Swift explained: Ignoring return values – onlinecode

@discardableResult in Swift explained: Ignoring return values – onlinecode

In this post we will give you information about @discardableResult in Swift explained: Ignoring return values – onlinecode. Hear we will give you detail about @discardableResult in Swift explained: Ignoring return values – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

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

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.

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.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

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!

<!– Disable cross link to Medium



Also published on Medium.

–>

 

Hope this code and post will helped you for implement @discardableResult in Swift explained: Ignoring return values – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US