PDA

View Full Version : How to have a reference to the data




chen8002004
Jun 14, 2011, 06:24 PM
I would like to have a seperate class holding the data. Two objects can read and update the data. Is it possible to have one object own the data and the other object hold a pointer to the data? The other object will not retain the data class. Will this solution cause any possible memory leak?



jnoxx
Jun 15, 2011, 02:48 AM
U could hold a singleton, and put the date in there :)
(look for SynthesizeSingleton.h)

xStep
Jun 15, 2011, 04:49 AM
A singleton might be overkill if you really just have the three objects.

You could have object A create and own object C, and then set an ivar in object B to equal the ivar in object A that points to object C. Just don't release object C from object B, unless you retain it there too.

chen8002004
Jun 16, 2011, 08:02 PM
I did what xStep told me to. These is no error when running, but I get some warnings in Build: “No ‘assign’, ‘retain’, or ‘copy' attribute is specified - ‘assign’ is assumed” and “Assign attribute (default) not appropriate for non-gc object property ‘iC’ “. Can anyone help me with this warning?
@interface B: NSObject{
C* iC;
}
@property C* iC;
@end

xStep
Jun 16, 2011, 09:24 PM
Typically for your property statement you'd define at least whether it will perform a copy, a retain of the object id or simple accept an assignment of the id value. I'll leave it to you to learn more (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html).

To remove the warnings, at minimum your code would look something like this;


@property (retain) C* iC;


or


@property (assign) C* iC;