Global NSString

I need to create an NSString, so I can set its value in one class and get it in another. How can i do this?

+5
source share
6 answers

if you write:

NSString *globalString = @"someString";   

anywhere outside a method, class definition, function, etc., it will be able to refer anywhere. (it's global!)

A file that accesses it will declare it as external

extern NSString *globalString;

This declaration means that it is being accessed from another file.

+9
source

NSString global variable for full iPhone / Apps project

To declare / define / use a global variable, follow these simple steps: -

  • NSObject "GlobalVars.h .m"
  • GlobalVars.h #import @implementation, -

    extern NSString * Var_name;

  • GlobalVars.m #import @implementation -

    NSString * Var_name = @"";

  • AppDelegate.h

    @property (, ) NSString * Var_name;

  • AppDelegate.m.

    @synthesize Var_name;

  • , ( .m), / GlobalVars.h .h , .

  • , .
+6

.

:

NSMutableString *myString = @"some funny string";

:

extern NSMutableString *myString;
+5

NSString, , , .

MyClass.h:

@interface MyClass : NSObject {}
     + (NSString *)myGlobalVariable;
     + (void)setMyGlobalVariable:(NSString *)val;
@end

MyClass.m:

@implementation MyClass
    NSString *myGlobalVariable = @"default value";

    + (NSString *)myGlobalVariable {
        return myGlobalVariable;
    }

    + (void)setMyGlobalVariable:(NSString *)val {
        myGlobalVariable = val;
    }
@end
+3

, . , int - NSObject.

. . , - .

+2
0
source

All Articles