发布配置中的转义闭包导致链接器错误

问题描述:

我正在开发iOS应用程序。发布配置中的转义闭包导致链接器错误

对于我的服务器API请求,我使用Alamofire包装方法,其中包括@escaping关闭 - 通常用于成功,失败和处理加载指示器。封闭本身在我的ViewControllers子类中声明为lazy var。这种封闭的一个例子:

lazy var sendRequestSuccess = {() -> Void in 
    // do something here 
} 

我怎么叫我的API包装方法:

APIRequestHelper.sharedInstance.sendRequest(success: requestSuccess, progress: animateActivityIndicator, failure: requestFailure) 
// APIRequestHelper is my class for all API requests 

而在一般情况下,我的API的方法是这样的:

func sendRequest(success: @escaping() -> Void, progress: @escaping (AnimationAction) -> Void, failure: @escaping (String?) -> Void) { 
// Alamofire request sending and response handling goes here 
} 

我的问题

当我在中构建我的应用程序时配置,一切都可以顺利建立并运行。然而,在我所有的@escaping倒闭(其中96到目前为止)Release配置生成链接错误,看起来像这样:

"__TFFC8<my app name>27AddressPickerViewControllerg23getPlaceLocationFailureFGSqSS_T_auL_4selfS0_", referenced from: 
     __TTSf4d___TFFC8<my app name>27AddressPickerViewControllerg23getPlaceLocationFailureFGSqSS_T_U_FGSqSS_T_ in AddressPickerViewController.o 
    "__TFFC8<my app name>26RequestsListViewControllerg22getRequestsListSuccessFGSqGSaCS_5Order__T_auL_4selfS0_", referenced from: 
     __TTSf4g___TFFC8<my app name>26RequestsListViewControllerg22getRequestsListSuccessFGSqGSaCS_5Order__T_U_FGSqGSaS1___T_ in RequestsListViewController.o 
    "__TFFC8<my app name>19LoginViewControllerg19getCountriesFailureFGSqSS_T_auL_4selfS0_", referenced from: 
     __TTSf4g___TFFC8<my app name>19LoginViewControllerg19getCountriesFailureFGSqSS_T_U_FGSqSS_T_ in LoginViewController.o 

一对夫妇更多的附加消息:

ld: symbol(s) not found for architecture arm64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我试过

根据其他几个提到链接器错误的SO问题,我确信我的“其他链接器标志”中的$(inherited)位于“生成设置”>“链接”中。实际上,DebugRelease配置的选项(列出的库)都相同。我也试过在Build Settings> Architectures中打开和关闭“Build Active Architecture Only”。 但是,大多数此类问题都与各种第三方库有关,而在我的案例中,@escaping关闭是语言本身的一部分,似乎会造成麻烦。

这是我的开发人员职业生涯中的第一个问题,它让我自己写了自己的SO问题,提前感谢。

我不得不自己找到解决方案,所以我会在这里写下来以防万一有人遇到同样的问题。

原来问题出在我的@escaping闭包语法中。我没有提供捕获列表,链接器也有问题,可能是由于可能存在保留周期。另外,因为我将在关闭时使用weak self,所以现在我必须使用可选链接来拨打self

所以我通过我所有的瓶盖去了,改变了他们的语法从

lazy var someOperationSuccess = {(someStringParameter: String?, someIntParameter: Int?) -> Void in 
      print ("Success") 
      self.doSomething() 
     } 

lazy var someOperationSuccess: (String?, Int?) -> Void = {[weak self] someStringParameter, someIntParameter in 
     print ("Success") 
     self?.doSomething() 
    }