Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:单例中的属性值_Ios_Objective C_Singleton - Fatal编程技术网

iOS:单例中的属性值

iOS:单例中的属性值,ios,objective-c,singleton,Ios,Objective C,Singleton,我的单件设置: //UserProfile.h @property (nonatomic, retain) NSString * firstName; @property (nonatomic, retain) NSString * lastName; +(UserProfile *)sharedInstance; //UserProfile.m +(UserProfile *)sharedInstance { static UserProfile *instance = nil;

我的单件设置:

//UserProfile.h
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;
+(UserProfile *)sharedInstance;


//UserProfile.m
+(UserProfile *)sharedInstance
{
    static UserProfile *instance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        if (instance == nil){
            instance = [[UserProfile alloc]init];
        }
    });
    return instance;
}
呼叫单身人士:

UserProfile *profileSharedInstance = [Profile sharedInstance];
profileSharedInstance = [responseObject firstObject];
NSLog (@"[UserProfile sharedInstance].lastName %@", [UserProfile sharedInstance].lastName);
NSLog (@"profileSharedInstance.lastName %@", profileSharedInstance.lastName);
日志:


问题:为什么[UserProfile sharedInstance].lastName为空?难道不是史密斯吗?

你的代码没有意义

UserProfile *profileSharedInstance = [Profile sharedInstance];
profileSharedInstance = [responseObject firstObject];
在这里,您的init正在创建对单例对象的静态引用。然后,您将通过引用从网络中获得的内容来覆盖它

在此点之后对[UserProfile sharedInstance]的任何调用都不会起作用,因为静态引用现在已不存在

您需要做的是创建一个方法,该方法将接收一个对象并设置其值。e、 g

[[UserProfile sharedInstance] setProfile: <profileObject>];
创建单例时,您要做的是让类为您保留指向对象的指针,因为该对象将在多个位置被引用,有点像一个全局变量。与全局变量不同,您不能简单地用其他对象替换对象。必须在init之后使用getter/setter来获取/更改值

问题:为什么[UserProfile sharedInstance].lastName为空?不是也应该是史密斯吗

因为它从未被设置为任何东西

这:

获取对单例的引用,然后用新对象替换该引用。所以,您实际上并没有一个单例实例,而是在alloc初始化另一个实例以在responseObject中返回

与其更改profileSharedInstance指向的对象,不如更新它包含的值。比如:

UserProfile *profileSharedInstance = [Profile sharedInstance];
profileSharedInstance.lastName = [responseObject firstObject].lastName;
这不是理想的,也不是有效的,但在这一行应该是可行的

UserProfile *profileSharedInstance = [Profile sharedInstance];  
设置局部变量并将其指向singleton

这条线

profileSharedInstance = [responseObject firstObject];
将局部变量重新指向[responseObject firstObject]

我想你想要的是

UserProfile *profileSharedInstance = [Profile sharedInstance]; 
UserProfile *responseProfile = [responseObject firstObject];
profileSharedInstance.firstName=responseProfile.firstName;
profileSharedInstance.lastName=responseProfile.lastName;

设置profileSharedInstance=[responseObject firstObject];,什么是responseObject?我可以做[UserProfile sharedInstance]=UserProfile*[responseObject firstObject];如何分配sharedInstance我从RESTkit获得的responseObject。谢谢。在这种情况下,理想的情况是创建一个填充共享实例的函数,至少在您的场景中是这样。
profileSharedInstance = [responseObject firstObject];
UserProfile *profileSharedInstance = [Profile sharedInstance]; 
UserProfile *responseProfile = [responseObject firstObject];
profileSharedInstance.firstName=responseProfile.firstName;
profileSharedInstance.lastName=responseProfile.lastName;