87

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

cit
  • 2,465
  • 5
  • 28
  • 36
REALFREE
  • 4,378
  • 7
  • 40
  • 73

7 Answers7

178

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

Visual example

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

howanghk
  • 3,070
  • 2
  • 21
  • 34
Ryan
  • 1,802
  • 1
  • 9
  • 6
  • 7
    This still doesn't work for me :( could there be another compiler flag that overrides this setting that I might have enabled? I do have these `-Wall -Wextra -Wno-unused-parameter` Warning flags enabled. – Kdawgwilk Mar 24 '16 at 05:45
  • 6
    @Kdawgwilk You sure you turn on that flag correctly? If its in your project you can just simply delete __weak keyword tho. I didnt try but you can also try to turn on the flag in Pod project if you are using Pod – REALFREE Mar 24 '16 at 16:38
  • 3
    Note if you currently run pod install / pod update then Weak References in Manual Retain Release is set to NO for each pod target - and you will have to edit the build settings again. – Damo Apr 21 '16 at 16:13
  • 2
    I had to do this in pod project setting as well. – Bernard Oct 06 '16 at 23:30
  • I had to set it to NO on the affected CocoaPod – Florian Feb 02 '18 at 12:47
21

This is official answer from Apple from the 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.

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

Update @ 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 __weak in non-ARC mode from now to avoid any unexpected conflicts. For cocoapods users, you do not need to delete __weak or wait for update but set Weak References in Manual Retain Release flag in build settings to YES like Lean said. Hope this help.

REALFREE
  • 4,378
  • 7
  • 40
  • 73
  • 1
    Note if you currently run pod install / pod update then Weak References in Manual Retain Release is set to NO for each pod target - and you will have to edit the build settings again. – Damo Apr 21 '16 at 16:13
21

The best way to solve this is to add a post_install script to your Podfile that sets the Weak References in Manual Retain Release flag to yes in all your pod targets. To do that just paste the following code at the bottom of your 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):

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
Community
  • 1
  • 1
villy393
  • 2,985
  • 20
  • 28
  • Great idea! It's working for me, because I'm using Cocoapods. Thanks. – mginius May 05 '16 at 17:21
  • 3
    I got follwing error: -fobjc-weak is not supported on the current deployment target – g212gs May 21 '16 at 09:53
  • I got the -fobjc-weak error also, but managed to fix it by setting all the pods deployment target to 8.3 (my project deployment target). You can do it with a script though, as suggested in the 2nd script above. – Xys Dec 13 '16 at 14:54
8

Workaround for Facebook weak references in FBSettings.m

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

 
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.

Manobala
  • 274
  • 2
  • 7
  • 1
    Just a note: Looks like FB have changed the offending line in v3.24.4, for the 3.x release line. (Not sure about the 4.x series, but it doesn't look like it's there now.) – big_m Jun 09 '16 at 20:05
7

I have found this.

I guess it's meant delete __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.)

Peter O.
  • 32,158
  • 14
  • 82
  • 96
lack
  • 121
  • 4
1

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"

peski
  • 21
  • 1
0

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