实际上,SharedInstance是什么? [英] What is SharedInstance actually?

查看:391
本文介绍了实际上,SharedInstance是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sharedInstance实际上是什么?我的意思是什么用途?

What is sharedInstance actually? I mean what is the usage?

当前我在2个不同文件之间进行通信时遇到了一些问题.

Currently I'm having some problem in communicating between 2 different files.

这是我的问题:

我有1个文件调用A.h/A.m和另一个文件调用B.h/B.m. A.h需要访问B.h中的某些数据,所以....有什么可能的方法可以实现我想要的?

I have 1 file call A.h/A.m and another file call B.h/B.m. A.h need to access some of the data in B.h, so .... is there any possible way I could achieve what I want?

奇怪的是它是"SharedInstance"吗?能够解决我的问题?

Just wonder is it "SharedInstance" able to solve my problem?

推荐答案

sharedInstance可以通过多种方式使用.

sharedInstance could be used for several ways.

例如,您可以从静态上下文访问对象.实际上,大多数方法都使用它来支持Singleton模式. 这意味着整个程序代码中仅使用该类的一个对象,而仅使用一个实例.

For example you can access an object from a static context. Actually it is used most ways for supporting the Singleton-pattern. That means that just one object of the class is used in your whole program code, just one instance at all.

接口看起来像:

@interface ARViewController
{
}
@property (nonatomic, retain) NSString *ARName;

+ (ARViewController *) sharedInstance;

实施ARViewController:

Implementation ARViewController:

@implementation ARViewController
static id _instance
@synthesize ARName;
...
- (id) init
{
    if (_instance == nil)
    {
        _instance = [[super allocWithZone:nil] init];
    }
    return _instance;
}

+ (ARViewController *) sharedInstance
{
    if (!_instance)
    {
        return [[ARViewController alloc] init];
    }
    return _instance;
}

要访问它,请在类CustomARFunction中使用以下内容:

And to access it, use the following in class CustomARFunction:

#import "ARViewController.h"

@implementation CustomARFunction.m

- (void) yourMethod
{
    [ARViewController sharedInstance].ARName = @"New Name";
}

这篇关于实际上,SharedInstance是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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