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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36147625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:37:42  来源:igfitidea点击:

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

objective-cxcodecocoapods

提问by REALFREE

After updating to Xcode 7.3, it throws the error Cannot create __weak reference in file using manual reference countingin pod files. Has anyone solved this issue?

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

回答by Ryan

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

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

Visual example

视觉示例

Taken from Apple Developers Forums - Xcode 7.3b4, non-arc, cannot create __weak reference.

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

回答by REALFREE

This is official answer from Apple from the link:

这是Apple从链接中得到的官方回答:

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.

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

So basically, if you are using Pod for 3rd party libraries, you have to either delete __weak in non-ARC or wait for update.

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

Update @ 3/23

更新@ 3/23

I should've research more about flags that I can pass to complier in order to bypass these kinda stuffs. But fundamentally you should not use __weakin non-ARC mode from now to avoid any unexpected conflicts. For cocoapods users, you do not need to delete __weakor wait for update but set Weak References in Manual Retain Releaseflag in build settings to YES like Lean said. Hope this help.

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

回答by villy393

The best way to solve this is to add a post_installscript to your Podfile that sets the Weak References in Manual Retain Releaseflag to yesin all your pod targets. To do that just paste the following code at the bottom of your Podfile.

解决此问题的最佳方法是post_install在 Podfile 中添加一个脚本,该脚本在所有 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

Sometimes, doing that result in the error -fobjc-weak is not supported on the current deployment target. You can solve that by adding another configuration option, forcing all the pods to target the version you want (based on this answer):

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

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

回答by Manobala

Workaround for Facebook weak references in FBSettings.m

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

To Podfile, it is possible to write a script to run after the pod install / update, describes the following there.

对于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 how to find the words of the magic that. Valid XHTML.

CLANG_ENABLE_OBJC_WEAK 怎么找那个魔法的话。 有效的 XHTML.

回答by lack

I have found this.

我找到了这个。

I guess it's meant delete __weak

我想这意味着删除 __weak

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

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

嗯,在 MRR [手动保留释放] 下有没有像弱变量引用这样的东西?“__weak”是指以下两种情况中的一种或两种:

  1. 无主引用(即不代表保留计数)。

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

#1 不适用于 MRR,因为您无论如何都不保留变量。

#2 也不适用于 MRR,因为运行时支持在 GC 和 ARC [自动引用计数] 中,您没有使用它们。

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

回答by peski

Just goto your target in "Build Phases" tab look for the pod files in "Compile Sources", click those files and add compiler flag "-fobjc-arc"

只需转到“构建阶段”选项卡中的目标,在“编译源”中查找 pod 文件,单击这些文件并添加编译器标志“-fobjc-arc”

回答by peski

Or change __weakto __unsafeunretained. This will solve the problem in tradition. Since MRC (before xCode 4 --) __weak was not in iOS.

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