'Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained

前端 未结 6 627
谎友^
谎友^ 2020-12-25 11:30

I\'m getting the error above, but unsure how to go about fixing it. This is my code:

.h:

#import 

@protocol ColorLineDelegate &         


        
相关标签:
6条回答
  • 2020-12-25 11:31

    Looks like your project might be using ARC then properties should be declared this way:

    #import <UIKit/UIKit.h>
    
    @protocol ColorLineDelegate <NSObject>
    -(void)valueWasChangedToHue:(float)hue;
    @end
    
    @interface ColorLine : UIButton 
    @property (nonatomic, weak) id <ColorLineDelegate> delegate;
    @end
    
    0 讨论(0)
  • 2020-12-25 11:31

    Use this syntax:

    @interface SomeClass  : NSObject {
        id <SomeClassDelegate> __unsafe_unretained  delegate;
    }
    @property (unsafe_unretained) id <SomeClassDelegate> delegate;
    
    0 讨论(0)
  • 2020-12-25 11:31

    I had the same problem when I used old example code which did not feature ARC in my ARC project. It seems that you do not need to put the variable declarations into the interface definition any more. So your code should work like this:

    h:

    #import <UIKit/UIKit.h>
    
    @protocol ColorLineDelegate <NSObject>
    
    -(void)valueWasChangedToHue:(float)hue;
    
    @end
    
    @interface ColorLine : UIButton {
    
        // Get rid of this guy!
        //id <ColorLineDelegate> delegate;
    }
    
    @property (nonatomic, assign) id <ColorLineDelegate> delegate;
    
    @end
    

    .m:

    #import "ColorLine.h"
    
    @implementation ColorLine
    
    @synthesize delegate;
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-25 11:53

    you can also use

    @dynamic delegate 
    

    in the implementation instead of synthesize.

    0 讨论(0)
  • 2020-12-25 11:54

    Perhaps a bit late but to be "ARC compliant", you simply have to replace

    @property (nonatomic, assign) id <ColorLineDelegate> delegate;
    

    by

    @property (nonatomic, strong) id <ColorLineDelegate> delegate;
    

    Bye.

    0 讨论(0)
  • 2020-12-25 11:58

    If you want a weak property, this also works.

    @interface MyClass  : NSObject {
        __weak id <MyClassDelegate> _delegate;
    }
    @property (nonatomic, weak) id <MyClassDelegate> delegate;
    
    0 讨论(0)
提交回复
热议问题