Ios - How to declare a static variable?

A static variable declared in C # is as follows:

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?

+7
source share
3 answers

Answer: use the static .

Syntax: static ClassName *const variableName = nil; (Updated [Added by const ] according to Abizern comment)


Reason for updating (according to comments from "Till") : static , when used for a variable in the / method function, its state will be preserved, even if the scope of this variable is left. When used outside any function / method, it will make this variable invisible to other source files - it will be visible inside this implementation file only when used outside any function / method. Therefore, const with static helps the compiler optimize it accordingly.

If you need more explanation when using const with static , I found here one nice link: const static .


Using:

You may have seen the use of the "static" keyword in the tableview delegate - cellForRowAtIndexPath:

static NSString *CellIdentifier = @"reuseStaticIdentifier";

+11
source
 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 eg dictionary keys. 

As far as I understand, public statics is not a built-in Objective-C function. You can get around this by creating a public class method that returns a static variable:

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

Most static objects cannot be initialized at compile time (I think that only lines are actually). If you need to initialize them, you can do this in the + (void)initialize method, which is lazily called when this 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]; } } 
+5
source

Objective-C is a super-set of C / C ++, so for static it follows from the C ++ / C convention, you can use it

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

Hoping you would try this way, do you have any mistake, if so, add more clarity to your question.

if in this case with NSString use the following command,

 static NSString *pString = @"InitialValue"; 

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

Hope that helps ...

+2
source

All Articles