1

I want to use Static Var to save a NSString. So I define a Static Var in a .h file like this:

#ifndef GlobalParameters_h
#define GlobalParameters_h

//access token
static NSString *applicationToken;

#endif

In class A, I change the static var like this:

#import "ClassA.h"
#import "GlobalParameters.h"

extern NSString *applicationToken;

@implementation ClassA

+ (void)parseResponse:(NSString *)response
{
    NSDictionary *responseDic = [response objectFromJSONString];

    NSString *token = [responseDic objectForKey:@"token"];

    applicationToken = [token copy];

    NSLog(@"%@",applicationToken);
}

When the debugger run to

applicationToken = [token copy];

I found the "applicationToken" is nil,but the next sentence

NSLog(@"%@",applicationToken);

can output the right value in console! And in ClassB , the "applicationToken" is nil too. I don't know why the static var is nil. I think the compiler will find the definition of "applicationToken" in GlobalParameters.h.But why I can't modify the static value?

Thanks for your help:)

2 Answers 2

2

static global variable mean that it's own for every object-file it's used. So there is willbe own applicationToken for ClassA, ClassB.

To create global variable for all object-files you need this:

In GlobalParameters.h:

#ifndef GlobalParameters_h
#define GlobalParameters_h

//access token
extern NSString *applicationToken;

#endif

In GlobalRarameters.m:

#import "GlobalParameters.h"

NSString *applicationToken;

P.S. I hope you use ARC, because if not, then applicationToken = [token copy]; will cause memory leaks.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your help.But,I use the keyword "extern". I remember that "extern" could expand the scope of the var.
@Tsich'i, ok. use extern, just don't use static.
In GlobalParameters_h , I modify "staic NSString *applicationToken" to "NSString *applicationToken" . Then in ClassA ,"applicationToken" can be modified correctly. Is there any posibilities the "applicationToken" be released during MyApp is running? @Cy-4AH
I hope "applicationToken" can be used during MyApp is running.
@Tsich'i, I have already wrote in which file how should be defined yours variable, why you doesn't use it?
0
  • Hey Its working fine , I am checking like this

    Once check the are you getting "responseDic" (or) not , Check the The dictionary have token key

   static NSString *applicationToken;
   applicationToken = @"srinivas";
   NSLog(@"%@",applicationToken);

   NSDictionary *responseDic = [NSDictionary dictionaryWithObject:@"static" forKey:@"token"];

   NSString *token = [responseDic objectForKey:@"token"];

   applicationToken = [token copy];

   NSLog(@"%@",applicationToken);
       [AppDelegate parseResponse:responseDic];

1 Comment

Yes, I check the value. The "token" has the correct value. But when I set the value to the static "applicationToken" , it's failure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.