Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static NSStrings in Objective-C

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~

like image 522
MikeyWard Avatar asked May 22 '10 13:05

MikeyWard


People also ask

What is NSString in Objective C?

(NSString *) is simply the type of the argument - a string object, which is the NSString class in Cocoa. In Objective-C you're always dealing with object references (pointers), so the "*" indicates that the argument is a reference to an NSString object.

What is NS string?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


4 Answers

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.

like image 64
harms Avatar answered Nov 08 '22 09:11

harms


Just stumbled upon the very same static NSString declaration. I wondered how exactly this static magic works, so I read up a bit. I'm only gonna address the static part of your question.

According to K&R every variable in C has two basic attributes: type (e.g. float) and storage class (auto, register, static, extern, typedef).

The static storage class has two different effects depending on whether it's used:

  • inside of a block of code (e.g. inside of a function),
  • outside of all blocks (at the same level as a function).

A variable inside a block that doesn't have it's storage class declared is by default considered to be auto (i.e. it's local). It will get deleted as soon as the block exits. When you declare an automatic variable to be static it will keep it's value upon exit. That value will still be there when the block of code gets invoked again.

Global variables (declared at the same level as a function) are always static. Explicitly declaring a global variable (or a function) to be static limits its scope to just the single source code file. It won't be accessible from and it won't conflict with other source files. This is called internal linkage.

If you'd like to find out more then read up on internal and external linkage in C.

like image 44
ksm Avatar answered Nov 08 '22 08:11

ksm


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.

like image 44
mipadi Avatar answered Nov 08 '22 09:11

mipadi


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]

like image 39
vodkhang Avatar answered Nov 08 '22 10:11

vodkhang