• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Building NSObject question/Help
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Building NSObject question/Help


  • Subject: Re: Building NSObject question/Help
  • From: Andrew White <email@hidden>
  • Date: Tue, 15 Mar 2005 10:27:18 +1100


Mark Thomas wrote:
Hi,
  I'm presently using a NSDictionary where I'm storing 2 NSStrings (value
and key values), but I'm going to need to store more than just a NSString
for the values part, so I thought I need either a store a struct or better
still a NSObject.

  As I guess if use a NSObject so storing this, and when the NSDictionary is
destroyed, it will call the destructor methods of my NSObject, so while I
know how I would do this in C++, can somebody give me some points how I
would do this in objective-c, looking at documentation do I just need to add
a dealloc method to my NSObject ???.

Firstly, NSDictionary is a Container, so it only stores objects of type NSObject (ie not C primitive types, including ints, arrays, or structs).


By inheriting from NSObject, you get the init and dealloc methods for free. Subclasses should override init and dealloc if they add any data members that need specific initialisation or deallocation. Like C++, local data just goes away, but dynamic data needs to be released.

All the Cocoa container classes automatically call 'retain' on data added to them and 'release' on data removed from them. The container does it's own memory management. The potential gotcha is if you keep a reference to an object in a container and don't retain it; if the object is removed from the container its reference count will go to 0 and the runtime system will free it.


Example 1:

@interface MyObject1: NSObject
{
	int x;
	char buf [10];
}
@end

Since nothing is dynamically allocated, NSObject's init and dealloc methods will work fine. You may want to override init if you want the data to start at something other than 0.


Example 1:

@interface MyObject2: NSObject
{
	int x;
	NSString * name
}
@end

Now you have dynamic data. You need to add a dealloc method to release name when you finish. Whether you add init depends on your initialisation requirements.

@implementation MyObject2

- (void) dealloc
{
	[name release];
	[super dealloc];
}

@end


The retain, release and autorelease methods are provided by NSObject. Don't touch them!


--
Andrew White

--------------------------------------------------------------------------
This email and any attachments are confidential. They may contain legally
privileged information or copyright material. You should not read, copy,
use or disclose them without authorisation. If you are not an intended
recipient, please contact us at once by return email and then delete both
messages. We do not accept liability in connection with computer virus,
data corruption, delay, interruption, unauthorised access or unauthorised
amendment. This notice should not be removed.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Building NSObject question/Help
      • From: Mark Thomas <email@hidden>
References: 
 >Building NSObject question/Help (From: Mark Thomas <email@hidden>)

  • Prev by Date: Re: Training in Australia?
  • Next by Date: Re: Dumb newbie question about objects and scalars
  • Previous by thread: Building NSObject question/Help
  • Next by thread: Re: Building NSObject question/Help
  • Index(es):
    • Date
    • Thread