4

I have protocol and class header in one file:

@protocol SomethingDelegate
- (void) doSomething;
@end

@interface SomethingClass
@property (nonatomic, weak) id <SomethingDelegate> delegate;
@end

On the .m file:

@implementation SomethingClass // in here I got error "Cannot synthesize weak property in file using manual reference counting"

@end

If I change it into like this:

@implementation SomethingClass 

@synthesize delegate; // in here I got error "Cannot synthesize weak property in file using manual reference counting"

@end

Why is this happened? And how to fix this? The error disappear if I change from weak to strong. But that's not how delegate should be declared, right? How to properly declare a weak delegate?

Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124

2 Answers2

22

You need to set Weak References in Manual Retain Release to YES in your Apple LLVM 8.0 - Language - Objective C under Build Settings of your project, as shown below in screenshot-

enter image description here

Sanjay Mohnani
  • 5,947
  • 30
  • 46
  • Thank you for the answer, but I'm developing using a legacy project settings, and I'm afraid to change any settings that might break the code. I believe new project in Xcode will already has this option turned `YES`, right? – Chen Li Yong Dec 09 '16 at 03:46
  • @ChenLiYong well legacy project..... i can understand, also this option is by default NO in a newly created project using Xcode 8, and we need to set it to YES. – Sanjay Mohnani Dec 09 '16 at 05:48
1

you don't have to have to use synthesize since Xcode 4.4 and LLVM 2.0 the compiler makes the synthesize automatically. you can remove the line

 @synthesize delegate; 

if you want to make the synthesize manually you can turn on "implicit synthesized properties" flag in your build settings by set it to YES

and you are using manual retain release you can't use weak/strong in your properties you should use retain/assign .

Marat Ibragimov
  • 1,076
  • 7
  • 7
  • 1
    Actually, when the compiler synthesise it automatically, it produces the same error like you see on my second code block. – Chen Li Yong Dec 08 '16 at 10:03
  • 2
    and change your property from weak to assign – Marat Ibragimov Dec 08 '16 at 10:07
  • okay thanks. So I guess I cannot use weak then. I build on top of a legacy project, so I guess that's explain the manual retain release I guess. – Chen Li Yong Dec 09 '16 at 03:47
  • As far as I know, "implicit synthesized properties" flag in the build settings is just a compiler warning flag so practically it doesn't change anything. To be able to synthesize weak properties in nonARC, "Weak References in Manual Retain Release" flag in the build setting should be set to YES. – s4mt6 May 19 '21 at 13:36