• 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: Mark Thomas <email@hidden>
  • Date: Mon, 14 Mar 2005 23:35:48 +0000
  • Organization: Coderus Ltd

Thanks Andrew, that's a lot clearer for what I need to do, I'm still
learning about objective-c and cocoa.

Does anybody how what I would need to do to get the NSObject so can be
stored and re-created when used with a NSDictionary when it used with the
standard preference APIs.

Thanks again in advance,
Mark.

>
> 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!

 _______________________________________________
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

References: 
 >Re: Building NSObject question/Help (From: Andrew White <email@hidden>)

  • Prev by Date: Re: Dumb newbie question about objects and scalars
  • Next by Date: Re: Web Services
  • Previous by thread: Re: Building NSObject question/Help
  • Next by thread: Rép : Problem of resolution with JPEGs
  • Index(es):
    • Date
    • Thread