Xcode 7.3不能在使用手动引用计数的文件中创build__weak引用

更新Xcode 7.3后,它会抛出错误Cannot create __weak reference in file using manual reference counting在pod文件中Cannot create __weak reference in file using manual reference counting 。 有没有人解决了这个问题?

设置Build Settings -> Apple LLVM 7.1 - Language - Objective C -> Weak References in Manual Retain ReleaseYES

视觉例子

从苹果开发者论坛 – Xcode 7.3b4,非弧,不能创build__weak引用 。

这是来自苹果链接的正式答案:

此问题的行为如下所示:我们正在实施所有Objective-C语言模式中的弱引用。 由于“__weak”历史上在非ARC(和非GC)语言模式中被忽略,所以我们添加了这个错误来指出未来语义将改变的地方。 请更新您的错误报告,让我们知道这是否仍然是您的问题。

所以,基本上,如果您使用的是第三方库的Pod,则必须删除非ARC中的__weak或等待更新。

更新@ 3/23

我应该研究更多关于我可以传给编译器的标志,以绕过这些东西。 但从根本上说,从现在开始,您不应该在非ARC模式下使用__weak以避免任何意外的冲突。 对于cocoapods的用户,你不需要删除__weak或者等待更新,但是可以将构build设置Weak References in Manual Retain Release标志中的Weak References in Manual Retain Release设置为YES,就像Lean说的那样。 希望这个帮助。

解决这个问题的最好方法是在您的Podfile中添加一个post_install脚本,将所有pod目标Weak References in Manual Retain ReleaseWeak References in Manual Retain Release设置为yes 。 要做到这一点,只需将以下代码粘贴到Podfile的底部。

 post_install do |installer_representation| installer_representation.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['CLANG_ENABLE_OBJC_WEAK'] ||= 'YES' end end end 

有时,这样做会导致-fobjc-weak is not supported on the current deployment target错误-fobjc-weak is not supported on the current deployment target 。 你可以通过添加另一个configuration选项来解决这个问题,强制所有的豆荚将目标定在你想要的版本上( 基于这个答案 ):

 post_install do |installer_representation| installer_representation.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['CLANG_ENABLE_OBJC_WEAK'] ||= 'YES' config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.3' end end end 

在FBSettings.m中的Facebook弱引用的解决方法

对于Podfile,可以编写一个脚本在pod安装/更新后运行,在那里描述以下内容。

 post_install do | installer | classy_pods_target = installer.pods_project.targets.find {| target | target.name == 'Facebook-iOS-SDK'} classy_pods_target.build_configurations.each do | config | config.build_settings['CLANG_ENABLE_OBJC_WEAK'] ||= 'YES' end end 

CLANG_ENABLE_OBJC_WEAK如何find那个魔法的话。 有效的XHTML

我find了这个。

我想这意味着删除__weak

https://forums.developer.apple.com/thread/38934

呃,在MRR [手动保留释放]下有没有这样一个弱variables参考? “__weak”是指两件事中的一件或两件:

  1. 无主的参考(即不代表保留计数)。

  2. 归零引用(即当被引用的对象被释放时,运行时归零)。

#1不适用于MRR,因为您只是不保留variables。

#2也不适用于MRR,因为运行时支持位于GC和ARC [自动引用计数]中,而不是您正在使用的。

这听起来像编译器现在只是抱怨说,它不能做它永远做不到的事情。 (而在应用程序委托的情况下,您将无法在运行时分辨出差异,因为应用程序委托通常不会被释放。)

只需在“Build Phases”选项卡中find目标,在“Compile Sources”中查找pod文件,单击这些文件并添加编译器标志“-fobjc-arc”

或者将__weak更改为__unsafeunretained 。 这将解决传统的问题。 由于MRC(在xCode 4之前)__weak不在iOS中。