Amazon.com Widgets Making a removeAll method, need help
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 02:48:07 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 15
| | | |-+  Making a removeAll method, need help
Pages: [1]   Go Down
Print
Author Topic: Making a removeAll method, need help  (Read 546 times)
tanakasan1734
Newbie
*
Posts: 9






« on: December 03, 2009, 02:22:11 PM »

Hi,

I'm trying to add to ex15.6 by making a removeAll method. Would the short method below work for this? I ask because when I call the list method on the book after I have apparently release all the address card objects the list shows up fine.

Code: (Objective-C)
-(void) removeAll {
for(AddressCard *nextCard in book) {
[nextCard dealloc];
}
}

I've tried running the code with the leaks instrument but I'm not sure how to interpret it.

Thanks for any help

Simon
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #1 on: December 03, 2009, 03:13:05 PM »

Simon,

Never call dealloc directly (the exception would be [super dealloc] when overriding the dealloc method).

To remove all address cards from the address book all you need to do is:
Code: (Objective-C)
[book removeAllObjects];
Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
tanakasan1734
Newbie
*
Posts: 9






« Reply #2 on: December 03, 2009, 03:24:31 PM »

Hi Ryan,

Thanks for the reply, and for pointing out dealloc (it was originally release but I changed it while playing around with Leaks).

So am I correct in thinking that this will release the memory used for the address cards as well?

I learnt C before jumping straight to Obj C so in my mind I'm used to having to explicitly release everything I allocate... I'm guessing I have to loose this mentality?

Thanks again

Simon
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #3 on: December 03, 2009, 03:55:02 PM »

Simon,

Quote
So am I correct in thinking that this will release the memory used for the address cards as well?

This will reduce the retain count by one. The memory won't be released until the retain count is zero.

When you added the cards to the book their retain counts were increased by one. Since you most likely allocated the memory for the cards yourself i.e. [AddressCard alloc] you are still responsible for sending another release message to the card.

Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.