ios/objective c/singleton: 在会话变量中存储用户标识
ios/objective c/singleton: Storing userid in session variable
我在单例中定义了一个 NSInteger(用于用户标识)并在另一个 class 中访问它。然而,虽然我设法消除了错误消息,因此它可以构建,但应用程序在运行时崩溃了。
这是初始定义;
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;
}
尝试在导入到 class
之上的其他 class 中获取会话
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
不是对象。它是原始 long
的 typedef
。所以删除对象指针。
typedef long NSInteger;
您似乎将 NSInteger
与 NSNumber
包装器混淆了。看看 docs for NSNumber.
以下代码使用 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);
这是我的标准会话对象
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
我在单例中定义了一个 NSInteger(用于用户标识)并在另一个 class 中访问它。然而,虽然我设法消除了错误消息,因此它可以构建,但应用程序在运行时崩溃了。
这是初始定义;
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;
}
尝试在导入到 class
之上的其他 class 中获取会话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
不是对象。它是原始 long
的 typedef
。所以删除对象指针。
typedef long NSInteger;
您似乎将 NSInteger
与 NSNumber
包装器混淆了。看看 docs for NSNumber.
以下代码使用 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);
这是我的标准会话对象
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