@discardableResult is not working as expected in framework

Viewed 740

I have met with a strange issue in my framework project. The Swift 3 compiler is warning me of an unused result when I try to call a function in my framework, even though I have marked the function in framework with @discardableResult

Here is my code SampleFramework.swift file in my cocoa touch framework project

import Foundation

@discardableResult
public func keychainPerform(key:String) -> String{
    print("success");
    return "aaa";
}

ViewController.swift file in single view application swift project

import UIKit
import SampleFramework

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        SampleFramework.keychainPerform(key: "asd") //  Warning: result of call is unused
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

I checked the scenario with other frameworks but @discardableResult is working perfectly on all of them, so please help me to find out what I am doing wrong?

1 Answers

I just stumbled over the very same problem in Xcode 12 / Swift 5.3.

I think this is a bug in the compiler with regards to interface file caching.

If you had the function previously created without the @discardableResult attribute, the compiler obviously does not pick it up after adding unless you do a full rebuild, i.e. force cleaning the build folder and/or delete the DerivedData.

Related