Static NSStrings in Objective-C

I often see code snippets like this in class instance methods:

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

I can’t understand why this is working. Is this just the equivalent of objc for #define, limited to the scope of the method? I (think) I understand the static nature of a variable, but more specifically about NSStrings, why not be alloc'd, init'd?

Thanks ~

+6
objective-c nsstring
source share
5 answers

For the NSString alloc, init part:

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

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

For the static and #define part:

A static instance in a class means that you can access any instance of the class. You can change the value of static. For a function, this means that a variable value is stored between function calls

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

+1
source share

I think the question has two unrelated parts.

First, why it is not appointed and not initiated. The answer is that when you write an Objective-C string literal of the form @"foo" , the Objective-C compiler will create an NSString for you.

Another question is what the static modifier does. It does the same as in the C function, ensuring that the myString variable myString same every time the method is used (even between different instances of the object).

Macro

A #define is completely different: it is a "programmatic cut and paste" of the source code, executed before the code arrives at the compiler.

+9
source share

Just stumbled upon the exact same static NSString declaration. I wondered how exactly this static magic works, so I read a little. I will only begin to consider the static part of your question.

According to K & R, each variable in C has two main attributes: type (for example, float) and storage class (auto, register, static, external, typedef).

The static storage class has two different effects, depending on whether it is used:

  • inside a code block (for example, inside a function),
  • outside all blocks (at the same level as the function).

A variable inside a block that does not have a declared storage class is considered automatic (i.e. local) by default. It will be deleted as soon as the block exits. When you declare an automatic variable static, it retains the value on exit. This value will still be present when the code block is called again.

Global variables (declared at the same level as the function) are always static. An explicit declaration of a global variable (or function) static limits the scope to only one source file. It will not be available, and it will not conflict with other source files. This is called intercom .

If you want to know more, read the internal and external links in C.

+4
source share

You do not see the alloc / init call, because the @"..." constructor creates a constant line in memory (via the compiler).

In this context, static means that it is not possible to access the variable from the file in which it is defined.

+2
source share

This is a special init case for NSString, which simply points to the NSS pointer to the instance allocated and launched at startup (or maybe lazy, I'm not sure.) There is one of these NSString instances created this way for every unique @ ", which you use in your program.

I also think that this is true even if you are not using a static keyword. Also, I think that all other NSStrings initialized by this line point to the same instance (not a problem, because they are immutable.)

This is not the same as #define, because in fact you have the NSString variable, creating a line with initialization = @ "whatever". This is more like c const char* somestr = "blah blah blah" .

+1
source share

All Articles