简体   繁体   中英

Storing Pointer to Another Object in NSMutableArray and Changing it

I have an object called GraphicPath which stores a CGMutablePath and a UIColor value for the fill color. I then have an NSMutableArray called graphic which stores a series of these GraphicPath objects which ultimately comprise an image that is drawn in drawRect of another class.

I also have a little module that pops up that lets the user change the color to this graphic. So I have a separate NSMutableArray that keeps track of all the colors in said graphic that interfaces with this module. Let's call it colors . I'd like to make it so whenever I make a change to colors , the corresponding color value in the GraphicPath also changes (so I don't have to go inside the Array of GraphicPaths to find it to change it).

So obviously the UIColor property in each GraphicPath property needs to have a pointer to a color in colors . I'm just so confused about how to keep a UIColor object pointer in the array colors , and make it so whenever I change this value, the corresponding value in a GraphicPath also changes.

As it is said in the comments, you need to make your GraphicPath object's UIColor field point to the corresponding object in the colors array. So that way changing of the object in the colors array will lead to "change" of the UIColor field of your custom object. For example, if you have:

UIColor *color1, color2, color3, etc;
NSMutableArray *colors = [[NSMutableArray alloc] initWithObjects:color1, color2, color3, nil];

NSMutableDictionary *graphicDictionary = [[NSMutableDictionary alloc] init];
[graphicDictionary setObject:somePathObject forKey:@"path"]
[graphicDictionary setObject:[colors objectAtIndex:0] forKey:@"color"];
GraphicPath *firstGraphicPathObject = [[GraphicPath alloc] initWithDitionary:graphicDictionary];

So next if you will change one of the color's objects, you will change the color# variables, and so you will change the value, returned by your objects field. Something like that I guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM