Swift 3.0
Result of call is unused
troubleshooting
error handling
duplicate post

Swift 3.0 Result of call is unused

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift 3.0, a common warning you might encounter is "Result of call is unused". This warning alerts you to the fact that the result of a function or method call is not being captured or used. This is a valuable feature for developers as it helps to ensure that important return values are not overlooked, potentially leading to logic errors or bugs in the application.

What Triggers the Warning?

The "Result of call is unused" warning is triggered when a function or method returns a value, but the return value is not assigned to a variable or used in some way. This often happens in scenarios where the developer may not be interested in the result of the call or is simply unaware that the function returns a result.

Example: Unused Result

swift
1func add(_ a: Int, _ b: Int) -> Int {
2    return a + b
3}
4
5add(5, 7) // Warning: Result of call to 'add' is unused

In the example above, the add function returns the result of the addition operation, but this result is not used. Swift issues a warning to remind you that the outcome of the function call might be important.

How to Resolve the Warning?

There are several ways to resolve the "Result of call is unused" warning in Swift:

  1. Use the Result: Assign the result to a variable or utilize it in an operation.
swift
let sum = add(5, 7)
print(sum)
  1. Explicitly Ignore the Result: If the result is genuinely not needed, you can explicitly ignore it using the underscore (_).
swift
_ = add(5, 7)
  1. Use @discardableResult: If you are writing a function and it is common for its return value to be ignored, you can annotate the function with @discardableResult. This tells the compiler that it is acceptable for the result to be ignored.
swift
1@discardableResult
2func subtract(_ a: Int, _ b: Int) -> Int {
3    return a - b
4}
5
6subtract(10, 3) // No warning, result can be discarded safely

When Should the Result Be Ignored?

Using @discardableResult should be considered carefully. It is most appropriate in cases where:

  • The primary behavior of the function does not depend on the return value. For example, methods performing work and only providing a status as a return.
  • The return value is often disregarded, such as in some fluent interface designs where the result of a method call is often irrelevant in the context.

Pitfalls of Ignoring Results

Blindly ignoring results can lead to less maintainable code or subtle bugs. For example:

  • Error Handling: If a function returns an error code or status that is indicative of failure, ignoring it might lead to unhandled error conditions.
  • Important Computation: Sometimes functions may perform calculations that are crucial for subsequent logic. Ignoring such results can lead to unexpected behavior.

Summary Table

TopicDescription
Issue"Result of call is unused" warning in Swift 3.0.
Occurs WhenThe result of a function or method call is not used or assigned.
Resolution Methods1. Use the result. 2. Explicitly ignore the result. 3. Use @discardableResult.
ConsiderationsOnly ignore results when it is appropriate; otherwise, always handle or use the return values.
Common Use CasesFunctions performing side-effects with optional result capturing, status or error code return values.

Conclusion

The "Result of call is unused" warning in Swift 3.0 serves as a beneficial reminder to developers to either use, ignore explicitly, or design functions with expected result-discarding in mind. By understanding the principles behind this warning, developers can produce clearer, more reliable, and more maintainable Swift code.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.