views:

407

answers:

4

I frequently see a code snippet like this in class instance methods:

static NSString *myString = @"This is a string.";

I can't seem to figure out why this works. Is this simply the objc equivalent of a #define that's limited to the method's scope? I (think) I understand the static nature of the variable, but more specifically about NSStrings, why isn't it being alloc'd, init'd?

Thanks~

A: 

For the part of NSString alloc, init:

I think first, it can be thought as a convenience, but it is not equally the same for [[NSString alloc] init].

I found a useful link here. You can take a look at that NSString and shortcuts

For the part of static and #define:

static instance in the class means you can access using any instance of the class. You can change the value of static. For the function, it means variable's value is preserved between function calls

#define is you put a macro constant to avoid magic number and string and define function macros. #define MAX_NUMBER 100. then you can use int a[MAX_MUMBER]. When the code is compiled, it will be copied and pasted to int a[100]

vodkhang
Why did I get a downvote here?
vodkhang
It wasn't me, but I do agree -- your answer is simply incomprehensible. See my answer, it breaks apart the original poster's question into three different topics (how `@"text"` literals are treated, what the `static` keyword does, and how this differs from a `#define`) and gives clear answers that hopefully people can learn something from.
harms
Ah, ok. I lacked the part of static. Because, at the first time, I only know about NSString alloc, init so I only answer for that part. I will edit my answer
vodkhang
Ok, I fixed it. Hope that it will be clear enough now
vodkhang
`static`, in this context, doesn't have anything to do with classes.
mipadi
I don't get it, static instance mean the same for instances in class, right?
vodkhang
The `static` keyword can mean lots of different things depending on the context. In this context we are talking about a variable declared `static` within a function, which simply means its value is preserved between calls to the function. The fact that the function also happens to be a method is irrelevant.
robinjam
Ok, I got it. Thanks, I see the word inside the method of the question
vodkhang
+1  A: 

It's a special case init case for NSString which simply points the NSString pointer to an instance allocated and inited at startup (or maybe lazily, i'm not sure.) There is one one of these NSString instances created in this fashion for each unique @"" you use in your program.

Also i think this is true even if you don't use the static keyword. Furthermore I think all other NSStrings initialized with this string will point to the same instance (not a problem because they are immutable.)

It's not the same as a #define, because you actually have an NSString variable by creating the string with the = @"whatever" initialization. It seems more equivalent to c's const char* somestr = "blah blah blah".

fsmc
+3  A: 

I think the question has two unrelated parts.

One is why isn't it being alloc'ed and init'ed. The answer is that when you write a Objective-C string literal of the @"foo" form, the Objective-C compiler will create an NSString instance for you.

The other question is what the static modifier does. It does the same that it does in a C function, ensuring that the myString variable is the same each time the method is used (even between different object instances).

A #define macro is something quite different: It's "programmatic cut and paste" of source code, executed before the code arrives at the compiler.

harms
+1  A: 

You don't see a call to alloc/init because the @"..." construct creates a constant string in memory (via the compiler).

In this context, static means that the variable cannot be accessed out of the file in which it is defined.

mipadi