This question already has an answer here:
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 ?
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:
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.Objective C is super set of C/C++ , so for static it follows C++/C convention, you should be able to use it
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,and if you have to modify
NSString
in your code, make sure it must beNSMutableString
.Hope that's help ...
Answer : use the
static
keyword.Syntax :
static ClassName *const variableName = nil;
( Updated [ Addedconst
] 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 aconst
withstatic
helps the compiler to optimize it accordingly.If you need more explanation on use of
const
withstatic
, 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";