@discardableResult无法在框架中按预期工作

aru*_*007 5 frameworks ios swift

我在框架项目中遇到了一个奇怪的问题。当我尝试在框架中调用函数时,Swift 3编译器会警告我未使用的结果,即使我已使用@discardableResult

这是SampleFramework.swiftcocoa touch framework项目中的代码 文件

import Foundation

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

ViewController.swiftsingle view application迅速项目中的文件

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.
    }



}
Run Code Online (Sandbox Code Playgroud)

我检查了其他框架的情况,但@discardableResult在所有框架上均能正常工作,所以请帮助我找出我做错了什么?

DrM*_*uer 0

我刚刚在 Xcode 12 / Swift 5.3 中偶然发现了同样的问题。

我认为这是编译器中关于接口文件缓存的错误。

如果您之前创建的函数没有该@discardableResult属性,则编译器显然不会在添加后选择它,除非您进行完全重建,即强制清理构建文件夹和/或删除DerivedData.