简体   繁体   English

PFUser currentuser在NSObject子类中不可用

[英]PFUser currentuser not available in NSObject subclass

I created a custom NSObject subclass so I could call something like MyObject1 *shared = [GlobalObjects myObject1]; 我创建了一个自定义的NSObject子类,因此可以调用MyObject1 * shared = [GlobalObjects myObject1];之类的东西。 as referenced in the accepted answer here: where to store "global" objects in iOS 如此处接受的答案所述: 在iOS中的“全局”对象存储位置

I have it setup, and it's making the call correctly, but for some reason it's not allowing me to access PFUser *currentUser = [PFUser currentUser] as it's returning null. 我已经设置好了,它正确地进行了调用,但是由于某种原因,它不允许我访问PFUser * currentUser = [PFUser currentUser],因为它返回的是null。 Right before I make my call for the GlobalObjects myObject1 I am able to access the user details with no problem, but as soon as it jumps to my subclass, it's null. 在我打电话给GlobalObjects myObject1之前,我可以毫无问题地访问用户详细信息,但是一旦它跳转到我的子类,它就为空。 I've included the Parse sdk and there are no errors, so I can't figure out what's going on here. 我已经包含了Parse sdk,并且没有错误,所以我不知道这里发生了什么。

Anyone have guidance on what I might be doing wrong? 有人对我可能做错的事情有指导吗?

For reference: 以供参考:

Initial view: 初始视图:

currentUser = [PFUser currentUser];
NSLog(@"%@",currentUser); // Outputs correctly
PFObject *settings = [GlobalObjects getSettings];

.h 。H

#import <Foundation/Foundation.h>
#import <Parse/Parse.h>

@interface GlobalObjects : NSObject

+(void)load;
+(PFObject*)getSettings;

@end

.m .m

#import "GlobalObjects.h"
#import <Parse/Parse.h>

static PFObject* _getSettings = nil;

@implementation GlobalObjects

+(void)load {
    PFUser *currentUser = [PFUser currentUser];
    NSLog(@"%@",currentUser); // Returns null always (I need to get the user here)
}
+(PFObject*)getSettings {
    return _getSettings;
}

@end

As for me your problem - in +(void)load method. 至于我,你的问题-在+(void)load方法中。 From the documentation to +[NSObject load] - order of initialization: 从文档到+ [NSObject load]-初始化顺序:

  1. All initializers in any framework you link to. 您链接到的任何框架中的所有初始化程序。
  2. All +load methods in your image. 图片中的所有+ load方法。
  3. All C++ static initializers and C/C++ attribute (constructor) functions in your image. 图像中的所有C ++静态初始化程序和C / C ++ 属性 (构造函数)功能。
  4. All initializers in frameworks that link to you. 链接到您的框架中的所有初始化程序。

So you is trying to get PFUser instance on step 2 but Parse framework will be proper initialized only on step 4. So just move your call from the load method to somewhere. 因此,您尝试在第2步获取PFUser实例,但仅在第4步才能正确初始化Parse框架。因此,只需将调用从load方法移至某个位置即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM