Xcode 7.3 无法使用手动引用计数在文件中创建 __weak 引用

Xcode 7.3 cannot create __weak reference in file using manual reference counting

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

我找到了。

我想这意味着删除 __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.)

这是 Apple 来自 link 的官方回答:

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.

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

更新 @ 3/23

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

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

摘自 Apple Developers Forums - Xcode 7.3b4, non-arc, cannot create __weak reference.

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

解决此问题的最佳方法是向 Podfile 添加一个 post_install 脚本,将所有 Pod 目标中的 Weak 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。您可以通过添加另一个配置选项来解决这个问题,强制所有 pods 以您想要的版本为目标 ():

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

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

FBSettings.m

中 Facebook 弱引用的解决方法

对于Podfile,可以在pod install / update 之后写一个脚本到运行,那里描述如下。

 
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怎么找那个神奇的话。 .