简体   繁体   中英

issue with custom delegate and datasource

I have a custom delegate and datasource. But I have several problems with it when I try to initialize it. In my .h file if have it like this.

@property (nonatomic, assign) id<UITableViewDelegate> delegate;
@property (nonatomic, assign) id<KalDataSource> dataSource;

This has as an result that in the synthesize in the .m file I get the following error.

Existing ivar 'dataSource' for property 'dataSource' with assign attribute must be __unsafe_unretained.

After some google search magic I found that I should assing my variables like this.

@property (nonatomic, strong) id<UITableViewDelegate> delegate;
@property (nonatomic, strong) id<KalDataSource> dataSource;

But then I get this error.

linker command failed with exit code 1 (use -v to see invocation)

Can anybody help me with this?

Kind regards!

The error you're experiencing has nothing to do with your memory qualifiers (they were right the first time around). The problem lies in the fact that you have declared a backing iVar somewhere without qualifiers. When iVars are declared, they are implicitly strong, so if you go to your shadowing iVars, and prepend __weak or __unsafe_unretained , the warning should disappear. Of course a better solution would be to just remove your backing iVars altogether, because Xcode will synthesize them for you.

Delegates are usually weak references.

The object using the delegate doesn't own it. It's just a reference to an object which could, or could not be responding. Weak says, that if the real owner of the object releases it, it should be deallocated. The weak reference is then automatically set to nil and you don't get any zombies.

Second, the problem is, that you already have property called dataSource .


EDIT

My previous statement about the duplicate property turns out to be wrong. You should override the setters & getters, both the declaration in the .h and the implementation in the .m file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM