ios/objective c/singleton: Storing userid in session variable

后端 未结 3 1529
执笔经年
执笔经年 2021-01-29 11:18

I defined an NSInteger in a singleton (for userid) and access it in another class. However, while I have managed to get rid of error messages so it builds, the app crashes when

3条回答
  •  暖寄归人
    2021-01-29 11:31

    Here is my standart session object

    H file

    @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 file

    //
    //  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
    

提交回复
热议问题