hi i am new to ios dev ,I am trying to set a value for nsstring from delegate class and access from other class ,the value i get is null.i dont know what mistake i am doing?

//token class header file
@interface TokenClass : NSObject
{
    NSString *tokenValue;
}
@property (nonatomic,strong) NSString *tokenValue;

//token class main file
@implementation TokenClass
@synthesize tokenValue;
@end

//App Delegate
TokenClass *token = [[TokenClass alloc]init];
[token setTokenValue:@"as"];

when i access tokenvalue in some other classs i get null value. can any one point me what mistake i am doing?Am i using @ property correctly?

有帮助吗?

解决方案 2

You need to use Singleton class to expose variables or objects to the entire project or create global variables. Create sharedInstance of TokenClass class and create property which can be accessed anywhere

in your .h file

//token class header file
@interface TokenClass : NSObject

@property (nonatomic,strong) NSString *tokenValue;

//create static method 
+ (id)sharedInstance;

in .m file

#import "TokenClass.h"

@implementation TokenClass


#pragma mark Singleton Methods

+ (id)sharedInstance {
    static TokenClass *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (id)init {
  if (self = [super init]) {
      tokenValue = [[NSString alloc] init];
  }
  return self;
}

@end

now in your appDelegate

#import TokenClass.h

@implementation AppDelegate

in `didFinishLaunchingWithOptions:`

 [TokenClass sharedInstance] setTokenValue:@"as"];

in any class you can get value using

NSLog(@"tokenValue = %@", [[SingletonClass sharedInstance] tokenValue]);

其他提示

There are a lot of ways to achieve what you want:

1. Usually I am using NSUserDefaults to save small amount of data which I will need even the user closed the app. There are a lot of information how to use it. See my answer here.

2. In your UIViewController class (e.x. your rootViewController) create @property which will hold your TokenClass. Then you will get tokenValue by self.tokenClass.tokenValue

3. The other way is create a singleton class which will be available during the whole run loop of your application. A Singleton candidate must satisfy three requirements:

  1. controls concurrent access to a shared resource.
  2. access to the resource will be requested from multiple, disparate parts of the system.
  3. there can be only one object.

    +(TokenClass*) sharedTokenClass {

    static dispatch_once_t pred;
    static TokenClass *_sharedTokenClass = nil;
    
    
        dispatch_once(&pred, ^{
            _sharedTokenClass = [[TokenClass alloc] init];
        });
        return _sharedTokenClass;
    }
    

    You will use it it from any place you want by [TokenClass sharedTokenClass]tokenValue];

If I were you, I would use the first variant.

PS. I strongly recommend you to read some memory management articles to get the point of object's lifecycle.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top