シングルトンと委任に関する十分な情報を読みました。だから、私はシングルトンとは何かを理解していると思います。委任について私はまだ混乱しています。委任の概念は理解していますが、委任を理解するためのプロトコルを作成する必要があります。
OK、CoreDataからエンティティを操作するためのシングルトンを作成します。たぶん私は間違っていて、それはシングルトンではありません、それについて教えてください。私のシングルトンはFetchDataです。
Fetchdata.h
#import <Foundation/Foundation.h>
@interface FetchData : NSObject <UIApplicationDelegate>
+(FetchData*) fetchData;
-(NSArray*)fetchLogin:(NSString*)name;
-(BOOL)newGroup:(NSString*)group forLogin:(NSString*)login;
-(NSMutableArray*)contactsForGroup:(NSString*)group;
-(BOOL)newContact:(NSString*)name surname:(NSString*)surname withDatas:(NSArray*)array;
//other methods
@end
Fetchdata.m
#import "FetchData.h"
#import "Contact.h"
#import "Login.h"
#import "Group.h"
#import "AppDelegate.h"
@interface FetchData ()
@property (nonatomic, strong) NSEntityDescription *loginEntity;
@property (nonatomic, strong) NSEntityDescription* groupEntity;
@property (nonatomic, strong) NSManagedObjectContext* context;
@property (nonatomic, strong) NSEntityDescription* contactEntity;
@property (nonatomic, strong) AppDelegate* appDelegate;
//other properties
@end
@implementation FetchData
@synthesize //my properties
+(FetchData*) fetchData
{
static FetchData* fetchData = nil;
if (!fetchData)
fetchData = [[super allocWithZone:nil]init];
return fetchData;
}
+(id)allocWithZone:(NSZone *)zone
{
return [self fetchData];
}
//implementation my methods
@end
そのため、CoreDataを使用するのは非常に簡単です。FetchDataをインポートするだけで、作成/削除/変更/追加/並べ替えのメソッドを使用できます...
SomeClass.m
#import "FetchData.h"
#define fetching [FetchData fetchData]
しかし、私は自分の目的の委任に使用できると思います。または、シングルトンと比較して、おそらくそれが最良の決定です。だから私は委任のためにシングルトンを作り直したいと思います。そして、私はこの質問について助けが必要です。私がしなければならないこと?
正しく理解していれば、FetchData.h、FetchData.mのすべてのメソッドを使用してプロトコルを作成する必要があります。変更せずに残すことができます。また、SomeClassでは、FetchDataをインポートしてプロトコルを追加する必要があります。好き:
#import <Foundation/Foundation.h>
@protocol FetchingDelegate
//all methods from FetchData.h
@end
@interface FetchData : NSObject
@property (nonatomic, strong) id <FetchingDelegate> delegate;
@end
FetchData.m
@interface FetchData()
//all properties without changing
@end
@implementation FetchData
@synthesize //all properties and delegate
//implementation of methods
@end
SomeClass
#import "FetchData.h"
@interface SomeClass : NSObject <FetchingDelegate>
@end
@implementation SomeClass
-(void)viewDidLoad
{
FetchData* fetching = [FetchData new]
fetching.delegate = self
}
//and now I can use any methods from protocol like [fetching anyMethod]
//such I used with singleton