Nez*_*jka 3 singleton delegates objective-c ios
我阅读了有关单身人士和授权的足够信息.所以,我想我明白什么是单身人士.关于代表团我仍然感到困惑.我理解授权的概念,但我需要创建我的协议以理解授权.
好的,我创建单例用于与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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
因此,现在可以很容易地使用CoreData.我只需要导入FetchData,只需使用创建/删除/更改/添加/排序方法...
SomeClass.m
#import "FetchData.h"
#define fetching [FetchData fetchData]
Run Code Online (Sandbox Code Playgroud)
但我认为我可以用于我的目标代表团.或者与单身人士相比,这可能是最好的选择.所以我想重新制作单身人士代表团.我需要这个问题的帮助.我应该做什么?
如果我理解正确,我需要使用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
Run Code Online (Sandbox Code Playgroud)
FetchData.m
@interface FetchData()
//all properties without changing
@end
@implementation FetchData
@synthesize //all properties and delegate
//implementation of methods
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
单身人士的想法是你的整个应用程序可以访问这一类.多个视图控制器可能需要来自数据库的数据.在你的情况下,我会改变你的fetchData方法(也许改变它的名字,因为它现在不遵循惯例):
+(FetchData*) fetchData
{
static FetchData *fetchData;
dispatch_once_t token;
dispatch_once(&token, ^{
if (!fetchData)
fetchData = [super init];
}
return fetchData;
}
Run Code Online (Sandbox Code Playgroud)
代表用于一对一通信,这意味着一个对象具有委托并将任何消息发送给该特定代表.
这意味着单身人士和代表团不能很好地融合在一起.单例用于向多个接收器发送消息,而委托模式用于一对一通信.因此,您有两个选择:您可以不使用单例并使用委托模式,也可以使用单例,并用于NSNotificationCenter通知观察者更改.
| 归档时间: |
|
| 查看次数: |
2928 次 |
| 最近记录: |