尝试使用委托时,在类型异常的对象中找不到属性“委托”

时间:2013-09-06 05:44:26

标签: ios objective-c delegates protocols

无法理解这种情况下的错误,所以我无法使用该委托 - self.plistManager.delegate = self; Property 'delegate' not found on object of type 'DataPlistManager *'

中存在例外情况
#import "DataManager.h"
#import "DataPlistManager.h"

@interface DataAdapter : NSObject <DataStorageManager>

@property (nonatomic,strong) DataPlistManager *plistManager;
- (void) saveFile;

@end

及其实施

#import "DataAdapter.h"

@implementation DataAdapter

-(id) initWithDataPlistManager:(DataPlistManager *) manager
{
    self = [super init];
    self.plistManager = manager;
    self.plistManager.delegate = self;
    return self;
}

- (void) saveFile
{
    [self.plistManager savePlist];
}

@end

3 个答案:

答案 0 :(得分:0)

您的DataPlistManager需要一个属性委托:

@property (weak) id<DataStorageManager> delegate;

答案 1 :(得分:0)

如果您在#import "DataAdapter.h"文件中添加"DataPlistManager.h",请将其删除并添加到"DataPlistManager.m"文件中,我不知道但是前几天我有同样的问题,我解决了通过使用这个技巧:)

答案 2 :(得分:0)

DataManager类包含委托属性,因此您应将对象设置为DataManager类的委托,并在委托类中调用方法(发送消息)saveFile:

@implementation DataAdapter

- (void)someMethod) {
    DataManager *dataManagerObject = [[DataManager alloc] init];
    dataManagerObject.delegate = self;
}

@implementation DataManager

- (void)someDelegateMethod {
    [self.delegate saveFile];
}

您确定了解委托模式的概念吗?

https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html