8

Static Variable declared in C# like this :

private const string Host = "http://80dfgf7c22634nbbfb82339d46.cloudapp.net/";
private const string ServiceEndPoint = "DownloadService.svc/";
private const string ServiceBaseUrl = Host + ServiceEndPoint;
public static readonly string RegisteredCourse = ServiceBaseUrl + "RegisteredCourses";
public static readonly string AvailableCourses = ServiceBaseUrl + "Courses";
public static readonly string Register = ServiceBaseUrl + "Register?course={0}";

How to call this static variable in another class ?

Bhavin
  • 27,155
  • 11
  • 55
  • 94
user2439531
  • 131
  • 1
  • 2
  • 7
  • 1
    As "static variable" is not a well defined term in Objective-C and different meanings are often confused it's not clear what you are asking here. Do you want a class variable with access through the class interface? Or is it about static storage duration? – Nikolai Ruhe Jun 06 '13 at 07:10
  • And here I was thinking that "static variable" is extremly well defined in Objective-C, with exactly the same meaning as in C. – gnasher729 Mar 18 '16 at 20:31

3 Answers3

12

Answer : use the static keyword.

Syntax : static ClassName *const variableName = nil; ( Updated [ Added const] as per the Comment by Abizern )


Reason for update ( As per the comments by "Till " ) : static when being used on a variable within a function/method will preserve its state even when the scope of that variable is left. When being used outside any function/method, it will make that variable invisible to other source files - it will be visible within that implementation file only when being used outside any function/method. Hence a const with static helps the compiler to optimize it accordingly.

If you need more explanation on use of const with static, I found one beautiful link here : const static.


Use:

You might have seen the use of "static" keyword in your tableview's delegate - cellForRowAtIndexPath:

static NSString *CellIdentifier = @"reuseStaticIdentifier";

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • 2
    I'd write `static NSString * const staticURL = @"something";` – Abizern Jun 06 '13 at 06:52
  • 2
    @Abizern: Can you explain me the reason behind `const` ? – Bhavin Jun 06 '13 at 07:02
  • 2
    @Vin that variable will never be changed during runtime, hence a const helps the compiler to optimize it accordingly. – Till Jun 06 '13 at 07:20
  • @Till: If that is the case then "What'll be the role of `static` in that line ?" or "`const` with `static` is just more optimum way to use `static`". – Bhavin Jun 06 '13 at 07:30
  • 1
    @Vin `static` when being used on a variable within a function/method will preserve its state even when the scope of that variable is left. When being used outside any function/method, it will make that variable invisible to other source files - it will be visible within that implementation file only when being used outside any function/method. – Till Jun 06 '13 at 07:34
  • @Till: Ah... Thanks for the Explanation. I'm Editing my Answer with **Abizern**'s line and your Explanation. – Bhavin Jun 06 '13 at 07:37
  • The way you used `const` makes the pointee const (an object of type `ClassName`), not the pointer. That's a concept in C++, yet it does not exist in Objective-C. If you want to do what you described (help the compiler to optimize usage of the variable you have to put the `const` to the right of the *: `static ClassName * const variableName = nil;`. That does not make sense either because it would prevent the variable of ever having another value than `nil`; – Nikolai Ruhe Jun 06 '13 at 08:30
5
static NSString *aString = @""; // Editable from within .m file
NSString * const kAddressKey = @"address"; // Constant, visible within .m file

// in header file
extern NSString * const kAddressKey; // Public constant. Use this for e.g. dictionary keys.

To my knowledge public statics aren't a built-in feature of Objective-C. You can work around this by making a public class method that returns the static variable:

//.h
+ (NSString *)stringVariable;

//.m
static NSString * aString;
+ (NSString *)stringVariable
{
    return aString;
}

Most static objects can't be initialized at compile time (I think only strings actually). If you need to initialize them, you can do so in the + (void)initialize method, which lazily gets called whenever that class is first referenced.

static UIFont *globalFont;
+ (void)initialize
{
    // Prevent duplicate initialize http://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html
    if (self == [ClassName class]) {
        globalFont = [UIFont systemFontOfSize:12];
    }
}
MaxGabriel
  • 7,617
  • 4
  • 35
  • 82
2

Objective C is super set of C/C++ , so for static it follows C++/C convention, you should be able to use it

static <<datatype>> <<variableName>> = initialization

Hoping you would have tried this way , have you got any error, if yes, please add more clarity in your question

if thats case with NSString use following,

static NSString *pString = @"InitialValue";

and if you have to modify NSString in your code, make sure it must be NSMutableString.

Hope that's help ...

Girish
  • 4,692
  • 4
  • 35
  • 55
Amitg2k12
  • 3,765
  • 10
  • 48
  • 97
  • If its a static why would you want to modify it? – Abizern Jun 06 '13 at 06:52
  • You have to explain more on this: to modify the string. Because this is valid also: `myString = [NSString stringWithFormat:@"Add string %@", myString]`; – pbibergal Jun 06 '13 at 06:54
  • 1
    Objective-C is a superset of C, no C++ involved. Actually the semantics of `static` slightly differ between C and C++. – Nikolai Ruhe Jun 06 '13 at 07:12