Global variable in Objective-C program

2019-01-14 08:48发布

I want store a number as a global variable. What syntax do I use, and how can other parts of my application access that variable?

6条回答
够拽才男人
2楼-- · 2019-01-14 09:10

To make a Variables that can be seen by the whole files in Objective c

for example you want to set your base url one time and in each class you append on it some extra strings,

go to main.m file, because this is the place the whole app will see it. then outside main function, put your base url

NSString *baseurl = @"staging.nabdanet.com";

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

and when you want to access these class, all what you will do is this,

SomeClass.m

extern NSString *baseurl;

with the same name in both ;)

查看更多
Animai°情兽
3楼-- · 2019-01-14 09:16

For a standard global variable (not persistent when the app is terminated and restarted) add this to a header file (*.h) of your choice:

extern NSInteger MYGlobalVariable;

Then put this in the implementation file; (*.m, *.c, *.cpp):

MYGlobalVariable = 0;  // Or any other default value.

That is how you do a bread and butter global variable.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-14 09:19

You probably want to use NSUserDefaults for this :

From anywhere in your code, you can set a value for a key :

int userAge = 21; // Just an example

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
    [standardUserDefaults setObject:[NSNumber numberWithInt:userAge] forKey:@"age"];
    [standardUserDefaults synchronize];
}

And get it back from any other place :

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *age = nil;

if (standardUserDefaults) 
    age = [standardUserDefaults objectForKey:@"age"];

userAge = [age intValue]

You can also set an initial value :

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary
    dictionaryWithObject:[NSNumber numberWithInt:13] forKey:@"age"];

[defaults registerDefaults:appDefaults];

Also, if you have complex data, you may want to create a wrapper class with setters and getters.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-14 09:28

For persistent vars, use NSUserDefaults. This gets written on a file in the app sandbox. For session vars (non-persistent), I use a singleton class with an NSMutableDictionary property to store variables.

查看更多
祖国的老花朵
6楼-- · 2019-01-14 09:29

To declare a global variable in either objective-C or Swift, you simply declare it outside the scope of any class/interface.

Objective-C:

#import "headerFile1.h"
#import "headerFile2.h"

BOOL isTrue = true;
int x = 1;

@interface exampleInterface (){
...  
}
@end

@implementation exampleClass
...

isTrue= false; // can be used in the same way anyplace in your code
x=3; // anyplace means anyplace, even from other controllers
@end

Swift:

import UIKit

var x=45;

class SomeClass {
... 
x=0; // This is also available from anyplace in your project
...
}
查看更多
迷人小祖宗
7楼-- · 2019-01-14 09:30

Define the variable in AppDelegate.h file. Create a property in .h file

@property (retain, nonatomic) NSString *str;

Then synthesize in AppDelegate.m file;

@synthesize str;

Later define a variable in you project prefix.pch file

#define DELEGATE ((AppDelegate*)[[UIApplication sharedApplication]delegate])

Use the value anywhere in your project

AppDelegate *a = DELEGATE;
a.str = @"value";
NSLog(@"value of variable str : %@",a.str);
查看更多
登录 后发表回答