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 ?
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";
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];
}
}
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With