ios / objective c / singleton:将userid存储在会话变量中

我在单例中定义了一个NSInteger(对于userid)并在另一个类中访问它。 然而,虽然我已经设法摆脱错误消息,因此它构建,应用程序崩溃时,它击中线。

这是最初的定义;

Session.h @interface Session : NSObject @property (assign,nonatomic) NSInteger *userid; + (IDSession *)sharedInstance; Session.m @implementation Session + (Session *)sharedInstance { static Session *session; if (!session){ session = [[Session alloc] init]; NSInteger *userid=1; //include this class in other class and reference userid with [Session sharedInstance].userid } return session; } 

尝试在导入上面的类的其他类中获取会话

 in save method: NSInteger *number =[Session sharedInstance].userid;//crashes here log says (lldb) 

非常感谢有关如何解决此问题的任何建议。

谢谢。

首先,我建议你按照这个习惯来创建一个线程安全的单例。

 + (Singleton *)sharedSettings{ static Singleton *singleton; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ singleton = [[self alloc] initPrivate]; }); return singleton; } - (instancetype)initPrivate{ self = [super init]; if(!self) return nil; return self; } 

此外, NSInteger不是一个对象。 它是原始longtypedef 。 所以删除对象指针。

typedef long NSInteger;

您似乎将NSIntegerNSNumber包装器混淆。 看一下NSNumber的文档 。

这是我的标准会话对象

H档

 @interface ControllerSession : NSObject @property(nonatomic, strong) NSString* sID; @property(nonatomic, strong) NSDate* sDate; #pragma mark sharedInstance + (ControllerSession*)sharedInstance; #pragma mark sessionVariables - (void)sessionVariableAddWithName:(NSString*)name WithValue:(id)value; - (id)sessionVariableGetWithName:(NSString*)name; @end 

M文件

 // // ControllerSession.m // FitTechs // // Created by Add080bbA on 19/04/16. // Copyright © 2016 Dayamatron. All rights reserved. // #import "ControllerModel.h" #import "ControllerSession.h" #import "ControllerSystem.h" //#import "includeListCategories.h" #import "NSObject+AppDelegate.h" @interface ControllerSession () @property(nonatomic, strong) ControllerSystem* ctrlSystem; @property(nonatomic, strong) ControllerModel* ctrlModel; @property(nonatomic, strong) Session* sessionEntity; //+ (NSMutableDictionary*)sessionVariables; @end @implementation ControllerSession @synthesize ctrlSystem; @synthesize ctrlModel; @synthesize sessionEntity; @synthesize sID; @synthesize sDate; #pragma mark sharedInstance static ControllerSession* objSelf; + (ControllerSession*)sharedInstance { if (!objSelf) { static dispatch_once_t onceTokenControllerSession; dispatch_once(&onceTokenControllerSession, ^{ objSelf = [[ControllerSession alloc] init]; }); } return objSelf; } #pragma mark init - (id)init { self = [super init]; if (self) { ctrlModel = self.appdel.ctrlModel; ctrlSystem = self.appdel.ctrlSystem; // session CoreData Stuff to record permanently sessionEntity = [ctrlModel sessionCreateBlank]; sessionEntity.systemOSVersion = [NSNumber numberWithFloat:ctrlSystem.osVersion]; sessionEntity.systemLocale = ctrlSystem.localeCurrent; sessionEntity.systemHardware = ctrlSystem.hardware; sessionEntity.systemLanguagesPrefered = [ctrlSystem languagesPreferedCombinedWithString:@";"]; sessionEntity.systemLanguageSelected = ctrlSystem.languageSelected; [ctrlModel saveContext]; } return self; } #pragma mark sessionVariables // session Temp Stuff to record static NSMutableDictionary* sessionDict; // singleton session dict object - (NSMutableDictionary*)sessionDictGet { if (!sessionDict) { static dispatch_once_t onceTokensessionDictGet; dispatch_once(&onceTokensessionDictGet, ^{ sessionDict = [[NSMutableDictionary alloc] init]; }); } return sessionDict; } - (void)sessionVariableAddWithName:(NSString*)name WithValue:(id)value { [self.sessionDictGet setObject:value forKey:name]; } - (id)sessionVariableGetWithName:(NSString*)name { return [self.sessionDictGet objectForKey:name]; } @end 

以下代码使用NSString而不是数字并设置局部变量而不是属性有效:

 Session.h @interface IDSession : NSObject @property (readonly, copy) NSString *userid; + (IDSession *)sharedInstance; @end Session.m #import "IDSession.h" @interface IDSession() @property (readwrite,copy)NSString * userid; @end @implementation IDSession + (IDSession *)sharedInstance { static IDSession *session; if (!session){ session = [[IDSession alloc] init]; //include this class in other class and reference userid with [IDSession sharedInstance].userid NSString * userid = @"1"; } return session; } @end in retrieving class. #import "session.h" NSString *userid =[Session sharedInstance].userid; NSLog(@"userid retrieved from session variable is %@",userid);