Add unretained object to an array

Discussion in 'Mac Programming' started by perrien, Oct 21, 2011.

  1. perrien macrumors newbie

    Joined:
    Apr 26, 2008
    #1
    I would like to be able to create an object and then add it to 2 or more arrays, dictionaries, etc, and if it's deleted from one, it should be deleted from the others automatically. So similar to this:

    Code:
    NSString *aString = [[NSString alloc] initWithString: @"Goodbye"];
    
    NSMutableArray *masterList = [NSMutableArray arrayWithObject: aString]; 
    NSMutableArray *copy = [NSMutableArray arrayWithObject: aString];
    
    [aString release];
    
    [masterList removeObjectAtIndex: 0];
    NSLog(@"Copy Count: %lu", [copy count]);  <- I'd like this to return 0
    I've tried adding:
    Code:
    [NSValue valueWithNonretainedObject: aString]
    to the copy array but the count remains 1 (as expected, the NSValue is still there), but I can't figure out how to tell if what is returned from
    Code:
    [[copy objectAtIndex: 0] nonretainedObjectValue]
    is valid. In the very small test programs I'm trying I'm getting odd results where it's not nil but anything I do with it causes a crash. Or the object works as if it had been retained, just a lot of strange, inconsistent behavior.

    If I could not even bother with the NSValue, that would be best. If it needs to be done, what would be the expected return from
    Code:
    [[copy objectAtIndex: 0] nonretainedObjectValue]
    if the object had been release elsewhere?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Please explain what you're trying to accomplish by doing this. In short, describe the problem, not your proposed solution (an automatically deleted object).

    I'm not even going to guess what the real problem is, because I can imagine several different scenarios, each with its own distinct solution.
     
  3. perrien thread starter macrumors newbie

    Joined:
    Apr 26, 2008
    #3
    This goes back to a question a couple days ago you assisted with. I have an 3D model that consists of a few hundred points and will be a convex hull of sorts. Each face is going to be an object (will have some methods, status info, etc) with an array of points that make up the face.

    When I cut off a section of this object, I'll be removing some points and adding new intersection points. Adding the new intersections to each face and the main list is doable but it would be nice to be able to then just remove a list of points from the Main and have all those points removed from the individual faces.

    If all of this sounds a bit muddled, this is a project I've had on the back burner for a few years now and have only recently figured out the math involved. The mathematics I now have figured out, now I'm down to actually laying code and organizing things. That organization is the next hurdle. So this one object will have maybe 10 tiers, each tier will have maybe 10 faces, each face will have usually 3-4 vertices. I'm trying to avoid the arrays of arrays of arrays or arrays of dictionaries which have arrays, etc. I'm having problems getting efficiency, elegance and understandability to come together into a solution.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    What's your current data structure? Post the @interface of each class. We probably don't need to see all the methods, though it can't hurt. Mainly need to see who has references to what other objects and types.

    Is your current data structure anything like one of the typical polygon mesh representations?
     
  5. perrien thread starter macrumors newbie

    Joined:
    Apr 26, 2008
    #5
    I'll hold off on that for now since the classes themselves, let alone the interfaces are still pretty fluid. The exact list of classes and the interfaces are exactly what it is I'm trying to nail down. I have a basic working model now but it's missing some things and I'm not really 100% happy with it. I think I just need to mull things over a bit more.

    I only took a quick look at that but it does appear to be in the right direction and will give me a bit more to work with. Thanks.
     

Share This Page