2016-03-22 19 views

回答

162

Set Build Settings -> Apple LLVM 7.1 - Language - Objective C -> Weak References in Manual Retain Release to YES

Visual example

Apple Developers Forums - Xcode 7.3b4, non-arc, cannot create __weak reference服用。

+0

我以为会有一个标志为它大声笑感谢 – REALFREE

+7

这仍然不适用于我:(可能会有另一个编译器标志覆盖此设置,我可能已启用?我有这些'--Wall -Wextra -Wno -unused-parameter' Warning flags enabled。 – Kdawgwilk

+6

@Kdawgwilk你确定你正确地打开该标志?如果它在你的项目中,你可以简单地删除__weak关键字tho。我没有尝试,但你也可以尝试打开Pod中的标志项目如果您正在使用Pod – REALFREE

7

我已经找到了。

我猜它的意思删除__weak

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

Erm, was there ever such a thing as a weak variable reference under MRR [manual retain-release]? "__weak" means one or both of two things:

  1. An unowned reference (i.e. not representing a retain count).

  2. A zeroing reference (i.e. that the runtime zeroes when the referenced object is deallocated).

#1 doesn't apply to MRR, because you just don't retain the variable anyway.

#2 doesn't apply to MRR either, because the runtime support is in GC and ARC [automatic reference counting], which you're not using.

It sounds like the compiler is now just complaining that it can't do what it could never do. (And in the case of an app delegate, you wouldn't be able to tell the difference at run-time, since the app delegate generally is never deallocated.)

19

这是官方的回答从苹果公司的链接:

This issue behaves as intended based on the following: We are in the process of implementing weak references in all Objective-C language modes. Since “__weak” has historically been ignored in non-ARC (and non-GC) language modes, we’ve added this error to point out places where the semantics will change in the future. Please update your bug report to let us know if this is still an issue for you.

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

更新@ 3/23

我应该研究更多关于我可以通过以编译绕过这些还挺充塞标志。但从根本上说,从现在开始,您不应该在非ARC模式下使用__weak以避免任何意外冲突。对于cocoapods用户,您不需要删除__weak或等待更新,但将生成设置中的Weak References in Manual Retain Release标志设置为YES,如Lean所述。希望这个帮助。

+0

请注意,如果您当前正在运行pod install/pod更新,那么对于每个pod目标,手动保留发布中的弱引用设置为NO - 并且您将不得不再次编辑构建设置。 – Damo

0

就跳转到你的目标在“构建阶段”选项卡中寻找在“编译来源”荚文件,点击这些文件,并添加编译标志“-fobjc弧”

17

解决这是最好的方法将post_install脚本添加到您的Podfile,将Weak References in Manual Retain Release标志设置为yes在您的所有Pod目标中。要做到这一点,只需将以下代码粘贴到您的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。您可以解决通过添加其他配置选项,迫使所有的吊舱的目标,你想要的版本(based on this answer):

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 
+0

Great ide一个!它为我工作,因为我正在使用Cocoapods。谢谢。 – mginius

+3

我收到了follwing错误: -fobjc-weak在当前部署目标上不受支持 – g212gs

+0

我也收到了-fobjc-weak错误,但设法通过将所有的pod部署目标设置为8.3(我的项目部署目标) 。您可以使用脚本来完成它,如上面第二个脚本所示。 – Xys

0

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

8

解决方法在FBSettings.m

Facebook的弱引用Podfile,可以编写一个脚本吊舱后,运行安装/更新,介绍了以下那里。


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如何找到这个魔法字的那个。 Valid XHTML

+0

很好的建议,谢谢! – HiveHicks

+1

只是一个说明:看起来像FB已经改变了v3.24.4中的违规行,对于3.x版本行。 (不知道4.x系列,但现在看起来不像它。) –