Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - How to declare static variable? [duplicate]

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 ?

like image 372
user2439531 Avatar asked Jun 06 '13 06:06

user2439531


3 Answers

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";

like image 82
Bhavin Avatar answered Oct 14 '22 02:10

Bhavin


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];
    }
}
like image 5
MaxGabriel Avatar answered Oct 14 '22 04:10

MaxGabriel


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 ...

like image 2
Amitg2k12 Avatar answered Oct 14 '22 04:10

Amitg2k12