Re: Pointers and NSImages
Re: Pointers and NSImages
- Subject: Re: Pointers and NSImages
- From: Erik Buck <email@hidden>
- Date: Thu, 2 Nov 2006 10:38:02 -0800 (PST)
You seem to have a very tenuous understanding of basic C programming concepts. You may be trying to solve a problem that is beyond your current skill level.
Nevertheless, all Objective-C objects are referenced by pointer. In your myPict = [[NSImage alloc] init... example, myPict is already a pointer. You presumably defined the variable someplace as NSImage *myPict; In C derived languages, you read the asterisk as "pointer". So the declaration is NSImage POINTER myPict;
If you change the image data stored by an NSImage instance, then all objects that have references to that image instance will immediately have access to the changes. For example, instead of creating a new image every time you make a change, just change the existing image. You can access the NSImageReps for an existing image and draw over them. You can also remove and add NSImageReps to an existing NSImage instance.
Now, from a design point of view, giving lots of objects a pointer to a mutable image that changes behind their backs is usually a terrible idea. The same is true for pointers to mutable strings or any other mutable objects. Furthermore, if you don't control all of the objects you give pointers to, some of them may copy the image when you set it. See the common accessor patterns at http://www.stepwise.com/Articles/Technical/2002-06-11.01.html/ and http://developer.apple.com/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_get_set/chapter_6_section_4.html. Also see X Code accessor generation and http://www.kevincallahan.org/software/accessorizer.html
The best answer to your question IMHO is that you should not give lots of objects pointers to the same NSImage. Instead, give lots of objects a pointer to an object that supplies an NSImage when asked.
@interface MYImageVendor : NSObject
{
}
- (NSImage *)provideCurrentImage;
@end
The -provideCurrentImage method can provide the appropriate image and even create a new image as needed whenever it is called by any number of callers.
_______________________________________________
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