客观C中的单身阶级 [英] singleton class in objective-C

查看:239
本文介绍了客观C中的单身阶级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个在代理中初始化的对象,我希望能够在视图控制器的任何地方使用此对象(不依赖于我目前的视图)。我猜这个解决方案是要有一个单身班,到目前为止,我有以下几点:

  @interface LocationManager: NSObject< CLLocationManagerDelegate> {
NSDate * enter;
NSDate * exit;
CLLocationManager * manager;

}

@property(nonatomic,retain)NSDate * enter;
@property(nonatomic,retain)NSDate * exit;

- (BOOL)registerRegionWithLatitude:(double)纬度和长度:(double)longitude;
+(LocationManager *)实例;

@end


#importLocationManager.h

@implementation LocationManager
@synthesize enter;
@synthesize exit;

#pragma mark - CLLocationManager委托
static LocationManager * gInstance = NULL;


+(LocationManager *)实例
{
@synchronized(self)
{
if(gInstance == NULL)
gInstance = [[self alloc] init];
}
return(gInstance);
}

@end

这是正确的吗?所以我需要做的只是访问这个只是调用实例? Inside LocationManager我也想只有一个CLLocationManager,叫做manager ..但是,我在哪里初始化它,所以我只有一个?我可以做以下吗?大多数其他单例示例在类中没有任何变量,所以我感到困惑

  +(LocationManager *)sharedLocationManager 
{
@synchronized(self)
{
if(lm == NULL){
lm = [[self alloc] init];
lm.manager = [[CLLocationManager alloc] init];
lm.manager.delegate = lm;
}
}
return(lm);
}


解决方案

基本上 - br>
只是几个小东西:

static LocationManager * gInstance = NULL;

而不是 NULL ,您应该使用 nil ,这是Objective-C中的惯例。



您还应该覆盖 alloc copyWithZone: mutableCopyWithZone:。从Buck / Yacktman:Cocoa Design Patterns,p。 153:

  +(id)hiddenAlloc 
{
return [super alloc];
}

+(id)new
{
return [self alloc];
}

+(id)allocWithZone:(NSZone *)zone
{
return [[self sharedInstance] retain];
}

- (id)copyWithZone:(NSZone *)zone
{
[self retain];
返回自我;
}

- (id)mutableCopyWithZone :( NSZone *)zone
{
return [self copyWithZone:zone];
}

这样,您的单例对象就无法复制。您需要从实例方法调用 hiddenAlloc (顺便说一下,访问Singleton对象的方法通常被称为Objective-C中的 sharedInstance


I want to have one object that is initialized in the delegate and I want to be able to use this object anywhere across view controllers (doesn't depend on what view I am currently at). I am guessing the solution to this would be to have a singleton class, so far I have the following:

@interface LocationManager : NSObject <CLLocationManagerDelegate>{
    NSDate *enter;
    NSDate *exit;
    CLLocationManager * manager;

}

@property (nonatomic, retain) NSDate * enter;
@property (nonatomic, retain) NSDate * exit;

- (BOOL)registerRegionWithLatitude:(double)latitude andLongitude:(double)longitude;
+ (LocationManager *)instance;

@end


#import "LocationManager.h"

@implementation LocationManager
@synthesize enter;
@synthesize exit;

#pragma mark - CLLocationManager delegate
static LocationManager *gInstance = NULL;


+ (LocationManager *)instance
{
    @synchronized(self)
    {
        if (gInstance == NULL)
            gInstance = [[self alloc] init];
    }
    return(gInstance);
}

@end

Is this correct? So all I need to do to access this is just to call instance? Inside LocationManager I also want to have only one CLLocationManager, called manager.. however, where do I initialize it so I only have one? Can I do the following? Most other singleton examples doesn't have any variables in the class, so that's where I got confused

+ (LocationManager *)sharedLocationManager
{
    @synchronized(self)
    {
        if (lm == NULL){
            lm = [[self alloc] init];
            lm.manager = [[CLLocationManager alloc] init];
            lm.manager.delegate = lm;
        }
    }
    return(lm);
}

解决方案

Basically -- yes.
Just a couple of small things:
static LocationManager *gInstance = NULL;
instead of NULL, you should use nil, it's a convention in Objective-C.

You should also overwrite alloc, new, copyWithZone:, and mutableCopyWithZone:. From Buck/Yacktman: "Cocoa Design Patterns", p. 153:

+ (id)hiddenAlloc  
{
  return [super alloc];
}

+ (id)new
{
  return [self alloc];
}

+ (id)allocWithZone:(NSZone *)zone
{
  return [[self sharedInstance] retain];
}

- (id)copyWithZone:(NSZone *)zone
{
  [self retain];
  return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone
{
  return [self copyWithZone:zone];
}

This way, your singleton object cannot be copied. You need to call hiddenAlloc from your instance method (by the way, the method to access a Singleton object is often called sharedInstance in Objective-C).

这篇关于客观C中的单身阶级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆