Objective-C Beginner (Issue) Question

I am just starting to learn Objective-C. I read Cocoa Become an XCoder book, and I think I learned the basics. Now I follow the online tutorial where I came across this bit of code:

@synthesize name;

- (IBAction)changeGreeting:(id)sender {
self.name = textInput.text;

NSString *nameString = name;
if([nameString length] == 0) {
    nameString = @"Cartman";
}
NSString *greeting = [[NSString alloc] 
                      initWithFormat:@"Hello, my name is %@!", nameString];
label.text = greeting;
[greeting release];
}

My question is here, should we not call 'release' also in the * nameString variable? Or, by doing this, will I also clear the name property that should be issued in the dealloc method? The reason, if I understand correctly, should I name "release" for all the variables located inside the functions at the end of these functions, but in the class properties should I call "release" only in the "dealloc" method?

thank

+3
source share
4 answers

, . , , . .

, alloc, copy, new mutableCopy. Apple/ . , , .

String.

+5

greeting , . nameString , . , alloc, .

. Apple. - iOS, .

, nameString, self.name , .

+2

Google Translate Obj-C :

@synthesize name;

: getter name .

self.name = textInput.text;

, textInput.text, setter, name.

NSString *nameString = name;

nameString , name. ( , , , .)

nameString = @"Cartman";

nameString , .

( , name:
name -> OBJECT,
nameString = name, nameString -> OBJECT,
nameString = OTHER_OBJECT, name -> OBJECT.)

NSString *greeting = [[NSString alloc] 
                  initWithFormat:@"Hello, my name is %@!", nameString];

NSString :.... greeting . alloc, . , release .

label.text = greeting;

, greeting , label, text. label , ; .

[greeting release];

, . , .

Objective-C.:) >

+2

The variable nameString is just a pointer to everything that is stored in the property of the class property. Thus, this string is technically stored in the memory block pointed to by String. When your method ends, the nameString pointer is simply cleared from memory. However, the name will still point to this memory. If you let go of the name String, it will clear the memory pointed to by that name, and therefore will give you problems later if you try to access the memory associated with the name.

0
source

All Articles