[英]iOS : Fail to use Global object class - Singleton
In my project I have an object MainAppDataObject
that I was to use globally between all other classes. 在我的项目中,我有一个对象
MainAppDataObject
,将在所有其他类之间全局使用该对象。 I want to keep other object in it which I need in different viewControls and can be accessed easily. 我想在其他viewControls中保留需要的其他对象,并且可以轻松访问。 I believe for this, I got to implement Singleton pattern.
我相信为此,我必须实现Singleton模式。
I tried implementing in 2 days ie using AppDelegate and GlobalObjects, but cannot access in other viewController or can't set the object to be saved. 我尝试在两天内实施,即使用AppDelegate和GlobalObjects,但无法在其他viewController中访问或无法设置要保存的对象。
Using GlobalObjects method : Header file : 使用GlobalObjects方法:头文件:
@interface MC_GlobalObjects : NSObject
+(void) load;
+(MainAppDataObject*) sharedAppDataObject;
@end
Implementation File .m : 实施文件.m:
#import "MC_GlobalObjects.h"
static MainAppDataObject* _sharedMainAppDataObject = nil;
@implementation MC_GlobalObjects
+(void) load {
_sharedMainAppDataObject = [[MainAppDataObject alloc] init];
}
+(MainAppDataObject*) sharedAppDataObject {
return _sharedMainAppDataObject;
}
@end
In my LoginViewController.m : 在我的LoginViewController.m中:
#import "MC_GlobalObjects.h"
MainAppDataObject *sharedAppObj = [MC_GlobalObjects sharedAppDataObject];
sharedAppObj.authLogin = logAgent;
I got the above reference from here , and I implemented as it says. 我从这里得到了以上参考,并且按照我所说的实现了。 I import MC_GlobalObjects.h, and just call sharedAppDataObject of MC_GlobalObject class.
我导入MC_GlobalObjects.h,然后仅调用MC_GlobalObject类的sharedAppDataObject。 MC_GlobalObject obj is not initialized anytime nor it's method load is called.
MC_GlobalObject obj不会随时初始化,也不会调用它的方法加载。 This line is the first time that I am using MC_GlobalObjects object from the start of the project.
从项目开始,这行是我第一次使用MC_GlobalObjects对象。 Result of the above code : sharedAppObj is created and it has authLogin as nil (as expected).
上面代码的结果:sharedAppObj已创建,并且authLogin为nil(如预期)。 When I assign logAgent to authLogin, authLogin is created but its contents remains nil.
当我将logAgent分配给authLogin时,会创建authLogin,但其内容仍然为零。 logAgent has values in it like full_name, job_title etc properties.
logAgent中具有诸如full_name,job_title等属性的值。
Can anyone help me know, why the values of logAgent object aren't being stored in authLogin. 谁能帮我知道,为什么logAgent对象的值没有存储在authLogin中。 Then I got to access authLogin in ProfileViewController.
然后,我在ProfileViewController中访问authLogin。
Is anything to correct in MainAppDataObject or AgentDetails obj. 在MainAppDataObject或AgentDetails obj中有什么要纠正的。 MainAppDataObject.h
MainAppDataObject.h
@interface MainAppDataObject : AppDataObject
{
AgentDetails *authLogin;
}
@property (nonatomic, copy) AgentDetails *authLogin;
Implementation File : .m 实施文件:.m
@synthesize authLogin;
AgentDEtails : .h AgentDEtails:.h
@interface AgentDetails : NSObject <NSCopying>
@property (nonatomic, copy) NSString *full_name;
@property (nonatomic, copy) NSString *job_title;
@property (nonatomic, copy) NSString *photo;
@property (nonatomic, copy) NSNumber *agent_id;
@property (nonatomic, copy) NSString *agentSecret;
@property (nonatomic) bool chatStatus;
@property (nonatomic) bool onlineStatus;
@property (nonatomic, copy) NSNumber *session;
-(id) initWithNSDictionary: (NSDictionary *)loginInfo;
@end
Implementation .m : 实施.m:
@synthesize full_name;
@synthesize job_title;
Where is the problem ? 问题出在哪儿 ? Can anyone please help me sort out.
谁能帮我解决一下。 I got access such many objects and data across many viewControllers and other objects.
我可以跨许多viewController和其他对象访问这么多对象和数据。 Any help is highly appreciated.
非常感谢您的帮助。 Thanks a lot.
非常感谢。 I have also referred other questions on the site referring the same query, but can't find a solution for the problem that I am facing.
我还引用了相同的查询在网站上提到了其他问题,但是找不到我所面临的问题的解决方案。 I have look around at [here], here 2 , here , here , etc.
我环顾[这里], 这里 2 , 这里 , 这里等。
A couple of things. 有几件事。 First, there's no reason to have a separate MC_GlobalObjects and MainAppDataObject classes.
首先,没有理由拥有单独的MC_GlobalObjects和MainAppDataObject类。 Make your MainAppDataObject object the singleton.
使您的MainAppDataObject对象成为单例。
Second, you don't need to, and should not, have a +load method. 其次,您不需要也不应使用+ load方法。 Make the sharedAppDataObject method lazily load the object if it isn't loaded already:
如果尚未加载sharedAppDataObject方法,请延迟加载该对象:
@interface MainAppDataObject: NSObject
@property (nonatomic, strong) NSObject *someObject;
@property (nonatomic, strong) NSObject *someOtherObject;
@property (nonatomic, strong) NSString *someString;
+(MainAppDataObject) sharedAppDataObject;
@end
And your .m file: 和您的.m文件:
static MainAppDataObject* _sharedAppDataObject = nil;
@implementation
+(MainAppDataObject) sharedAppDataObject;
{
if (_sharedAppDataObject == nil)
_sharedAppDataObject = [[MainAppDataObject alloc] init];
return _sharedAppDataObject;
}
And then to use it in any class in your project, first #import the header, then use code like this: 然后在项目的任何类中使用它,首先#import头,然后使用如下代码:
[MainAppDataObject sharedAppDataObject].someString = @"Some string";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.