Hi Im new at iPhone SDK and i have found these lines in the book "Beginning iPhone 3 development":
NSString *title =[sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:@"%@buttonpressed!",title];
statusText.text =newText; (statusText is UILabel *statusText; and is placed inside @interface...its not allocated)
[newText release];
my questions:
-Ive learnd to create strings like that: NSString *str=@"example";
this isnt allocated..so why is newText allocated?
-title isnt a string of *str type?it isnt allocated right?
-when I release newText is statusText.text released too?If yes,then why is [statusText release]; later?statusText was never allocated at @interface..
Hi Im new at iPhone SDK and i have found these lines in the book "Beginning iPhone 3 development":
NSString *title =[sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:@"%@buttonpressed!",title];
statusText.text =newText; (statusText is UILabel *statusText; and is placed inside @interface...its not allocated)
[newText release];
my questions:
-Ive learned to create strings like that: NSString *str=@"example";
this isnt allocated..so why is newText allocated?
-title isnt a string of *str type?it isnt allocated right?
-when I release newText is statusText.text released too?If yes,then why is [statusText release]; later?statusText was never allocated at @interface..
thanks alot for your help
Q:-Ive learnd to create strings like that: NSString *str=@"example";
A: That is creating a constant string. Sort of like static char* foo = "foobar"; in c.
Q: this isnt allocated..so why is newText allocated?
A: newText isn't a static string. It's a string created by calling the NSString method initWithFormat.
Q: -title isnt a string of *str type?it isnt allocated right?
A: title is a string that's returned by calling a button's "senderForState" method. That method returns an autoreleased object. It was allocated by the caller, and then sent an "autorelease" message. The autorelease method tells an object that it should be released the next time the app visits the event loop. You use autoreleased objects as throw-aways. They persist until the last call in the call chain that created them returns, and the system drops back to the main event loop. At that point, an object called the "autorelease pool" gets "drained", which means that all objects that were sent autorelease messages get a release message for every autorelease they got in the last pass through your code. If any object's retain count drops to 0 while draining the autorelease pool, it gets deallocated.
There is a strong convention in Cocoa. If a method name returns an object and it's name starts with "new" or "alloc", or contains "copy", you own it, and are responsible for releasing it when you are done with it. All others return objects that are either still owned by the called object, or have been autoreleased, and will be discarded if you don't retain them.
Q:-when I release newText is statusText.text released too?
A: No. statusText is a probably a UILabel object. It has a "text" property that lets you assign strings to it. It's string property is almost certainly set up to make a copy of the string, which means that once you set statusText, you don't need newText any more, and can release it.
Q:If yes,then why is [statusText release]; later?statusText was never allocated at @interface..
The only time you would want to release statusText is if you retained it when you created it, and you were removing it from the view it was a part of. Usually UI objects are owned by their parent view "superview" and you don't have to do any memory housekeeping with them. When you release the top-most view of a view hierarchy, it releases it's subviews, which in turn release their subviews, causing the whole mess to be thrown away.
-----------------
You should stop what you are doing, go to XCode, search on "Memory Management Programming Guide", and read the entire document, think about it, then read it again from beginning to end. Perhaps even print it out and mark it up with a highlighter. It's probably the single most important thing you need to understand about programming in Cocoa, and until you get it, you'll write apps that crash unexpectedly, or leak like a sieve.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Q:-Ive learnd to create strings like that: NSString *str=@"example";
A: That is creating a constant string. Sort of like static char* foo = "foobar"; in c.
Q: this isnt allocated..so why is newText allocated?
A: newText isn't a static string. It's a string created by calling the NSString method initWithFormat.
Q: -title isnt a string of *str type?it isnt allocated right?
A: title is a string that's returned by calling a button's "senderForState" method. That method returns an autoreleased object. It was allocated by the caller, and then sent an "autorelease" message. The autorelease method tells an object that it should be released the next time the app visits the event loop. You use autoreleased objects as throw-aways. They persist until the last call in the call chain that created them returns, and the system drops back to the main event loop. At that point, an object called the "autorelease pool" gets "drained", which means that all objects that were sent autorelease messages get a release message for every autorelease they got in the last pass through your code. If any object's retain count drops to 0 while draining the autorelease pool, it gets deallocated.
There is a strong convention in Cocoa. If a method name returns an object and it's name starts with "new" or "alloc", or contains "copy", you own it, and are responsible for releasing it when you are done with it. All others return objects that are either still owned by the called object, or have been autoreleased, and will be discarded if you don't retain them.
Q:-when I release newText is statusText.text released too?
A: No. statusText is a probably a UILabel object. It has a "text" property that lets you assign strings to it. It's string property is almost certainly set up to make a copy of the string, which means that once you set statusText, you don't need newText any more, and can release it.
Q:If yes,then why is [statusText release]; later?statusText was never allocated at @interface..
The only time you would want to release statusText is if you retained it when you created it, and you were removing it from the view it was a part of. Usually UI objects are owned by their parent view "superview" and you don't have to do any memory housekeeping with them. When you release the top-most view of a view hierarchy, it releases it's subviews, which in turn release their subviews, causing the whole mess to be thrown away.
-----------------
You should stop what you are doing, go to XCode, search on "Memory Management Programming Guide", and read the entire document, think about it, then read it again from beginning to end. Perhaps even print it out and mark it up with a highlighter. It's probably the single most important thing you need to understand about programming in Cocoa, and until you get it, you'll write apps that crash unexpectedly, or leak like a sieve.
thank you so much for your time!! I appreciate it!!
Q:-Ive learnd to create strings like that: NSString *str=@"example";
A: That is creating a constant string. Sort of like static char* foo = "foobar"; in c.
Q: this isnt allocated..so why is newText allocated?
A: newText isn't a static string. It's a string created by calling the NSString method initWithFormat.
Q: -title isnt a string of *str type?it isnt allocated right?
A: title is a string that's returned by calling a button's "senderForState" method. That method returns an autoreleased object. It was allocated by the caller, and then sent an "autorelease" message. The autorelease method tells an object that it should be released the next time the app visits the event loop. You use autoreleased objects as throw-aways. They persist until the last call in the call chain that created them returns, and the system drops back to the main event loop. At that point, an object called the "autorelease pool" gets "drained", which means that all objects that were sent autorelease messages get a release message for every autorelease they got in the last pass through your code. If any object's retain count drops to 0 while draining the autorelease pool, it gets deallocated.
There is a strong convention in Cocoa. If a method name returns an object and it's name starts with "new" or "alloc", or contains "copy", you own it, and are responsible for releasing it when you are done with it. All others return objects that are either still owned by the called object, or have been autoreleased, and will be discarded if you don't retain them.
Q:-when I release newText is statusText.text released too?
A: No. statusText is a probably a UILabel object. It has a "text" property that lets you assign strings to it. It's string property is almost certainly set up to make a copy of the string, which means that once you set statusText, you don't need newText any more, and can release it.
Q:If yes,then why is [statusText release]; later?statusText was never allocated at @interface..
The only time you would want to release statusText is if you retained it when you created it, and you were removing it from the view it was a part of. Usually UI objects are owned by their parent view "superview" and you don't have to do any memory housekeeping with them. When you release the top-most view of a view hierarchy, it releases it's subviews, which in turn release their subviews, causing the whole mess to be thrown away.
-----------------
You should stop what you are doing, go to XCode, search on "Memory Management Programming Guide", and read the entire document, think about it, then read it again from beginning to end. Perhaps even print it out and mark it up with a highlighter. It's probably the single most important thing you need to understand about programming in Cocoa, and until you get it, you'll write apps that crash unexpectedly, or leak like a sieve.
one more thing...how do you know all these about what these methods return(autorelease for example)?is there an index where i can see too?