the object will be destroyed. • Note that the value of -‐retainCount is not reliable for debugging purposes. -‐retainCount NSString *mystr = [[NSString alloc] init]; +1 1 [mystr retain]; +1 2 [mystr release]; -‐1 1 [mystr release]; -‐1 0
method will be called. • Always call [super dealloc] in your custom -‐dealloc method. This is the only place where you are allowed to call -‐dealloc. • Release all objects that your object owns in -‐ dealloc. Tip: Add NSLog() calls in -‐dealloc if you want to be sure if your object gets destroyed.
object pointed by caption! [caption release]; ! ! // Point caption to value and retain it.! // The –retainCount is now +1! caption = [value retain]; ! } public function setCaption($value) { $this->caption = $value; } The Photo class is now “part-‐owner” of value
an object that you “own” • Recommended for small objects like NSString and NSNumber • Since you own copied objects, you should be the one to release it. - (void)setCaption:(NSString *)value! {! // Release the object pointed by caption! [caption release]; ! ! // Create a copy of value. The -retainCount is now "1"! caption = [value copy]; ! }
an object but not instantly. • The object is added to the current autorelease pool Autorelease Autorelease pool (NSAutoreleasePool) • Tracks objects that are autoreleased and releases all of them when the pool itself is released • UIKit wraps sets an autorelease pool on every event (e.g. buZon click)
*caption;! }! ! - (NSUInteger)userId;! - (void)setUserId:(NSUInteger)userId;! ! - (NSString *)caption;! - (void)setCaption:(NSString *)value;! ! @end The old standard way
class named FUser that has these proper&es: • firstName (String) • lastName (String) • canCreateUsers – a readonly BOOL. Returns NO • fullName – Readonly. returns a concatenated firstName and lastName. Use +alloc and -‐init and -‐autorelease in this case. Create a subclass of FUser named FAdminUser. For FAdminUser, canCreateUsers should be YES. The fullName property should append “ (Admin)”. Important: • The fullName property should make use of the returned value of the superclass ([super fullName]).