Frost
Frost

Reputation: 3896

confused about NSString *str

I am kind of confused by the behavior of NSString *str..

I assigned it in several ways, sometimes it works, and sometimes it becomes null.

NSString *str = @"/hi/hello"; // this one always works
// this sometimes becomes null after the function ends
NSString *str2 = [str lastPathComponent]; 
// as above
NSString *str3 = [NSString stringWithString:str2];
NSString *str4 = [NSString initWithString:str3];

I am not quite familiar with the object behavior of Obj-C, is it just like C++?

If so, how can I do assignment safely like

string str = "hi";
string str2 = str;

behaves in C++?

ex: I declare a string in my .h file,

how to assign it safely that it wouldn't become NULL after a function ends?

I know it's a very basic question, but I can't find the answer in NSString reference page.

Really thanks for any help!

Upvotes: 1

Views: 369

Answers (3)

walkytalky
walkytalky

Reputation: 9543

The behaviour is not just like C++. Objects are reference-counted. If you want to keep one around, you must place a claim on it.

If you create the object yourself with a method whose name includes the word alloc, new or copy then you have ownership already. This is like a C++ new. (When you have created an object with alloc, you need also to initialise it with some method whose name begins init. But you have to create it first. In C++ both things would be considered parts of the single act of construction.)

Objects you receive from other methods (such as two of the three NSString methods you mention) are only transiently available unless you explicitly claim ownership by calling [object retain]. You only need to do this if you want to keep them around beyond the immediate context. (There isn't really an equivalent to this in C++.)

However you gain ownership, you must relinquish it when you are finished by calling [object release]. This sort of like a C++ delete, except that the object isn't actually destroyed until all ownership claims are released.

Getting to grips with this is really really really important, perhaps the only important thing you need to know to use Objective-C. Read the object ownership documentation carefully and you'll be sorted.

Upvotes: 7

Ira Cooke
Ira Cooke

Reputation: 1345

NSString *str = @"/hi/hello"; 

This works because it creates a string literal. Answers to this question are worth a read to understand these in Objective-C

What's the difference between a string constant and a string literal?

In all these cases you are creating autoreleased strings. These will be deallocated when you return to the application's runloop.

NSString *str2 = [str lastPathComponent]; 
NSString *str3 = [NSString stringWithString:str2];

In this last one I assume you meant [[NSString alloc] initWithString:str3]

This creates a string that is retained. But this isn't a good way to create static strings. You should create static strings in your implementation file like this

static NSString *myConstant = @"constantString"

Upvotes: 2

Gary
Gary

Reputation: 5732

I assume you're not using garbage collection? If this is the case then you need to retain the string.

NSString* str2 = [[str lastPathComponent] retain];

I suggest you do some reading on objective-c memory management.

Upvotes: 2

Related Questions