开发者

iOS - Passing a NSMutableArray via method return

How would one pass a NSMutableArray via method return.

I have it passing the array "spaces" so an array of 10 objects passes the 10 blocks but none of the information contained in those objects.

Thanks in advance

Edit: Basically I created another class that contains path information because my controller was getting a bit cluttered. So this new clas开发者_如何学Cs I want call the "create" method which returns an NSMutableArray. The array is created fine in the path class but when the return statement fires it only passes the spaces and not the values or even a pointer.

currently it's

return path;

I've tried

return &path; 

and that fails epically.

Edit2: Here is the issue I'm having unfortunately.

iOS - Passing a NSMutableArray via method return

iOS - Passing a NSMutableArray via method return

Still crashing

calling

newNode = [newNode copy]; 

causes a crash


- (NSMutableArray *) mutableFloobizwits {
  NSMutableArray *array = [NSMutableArray array];
  for (NSInteger i = 0; i < TheAnswerToTheUltimateQuestion; ++i) {
    void(^MyBlock)(void) = ^{
      NSLog(@"captured i: %ld", i);
    };
    MyBlock = [MyBlock copy];  //move the block from off the stack and onto the heap
    [array addObject:[Floobizwit floobizwithWithBlock:MyBlock]];
    [MyBlock release]; //the Floobizwit should've -retained the block, so we release it
  }
  return array;
}


I would set up your other class that returns the array of path objects as follows:

@implementation PathFactory

- (NSMutableArray*) create
{
    // In your PathFactory object you create an array and make it autorelease so 
    // it becomes the callers responsibility to free the memory 
    NSMutableArray * pathArray = [[[NSMutableArray alloc] init] autorelease];

    // Create a bunch of PathObject objects and add them to the mutable array
    // also set these to autorelease because the NSMutableArray will retain objects
    // added to the collection (ie It is the NSMutableArray's responsibility to ensure
    // the objects remain allocated).
    for (int i = 0; i < numberOfPaths; i++)
        [pathArray addObject:[[[PathObject alloc] init] autorelease]];

    // Just return the pointer to the NSMutableArray. The caller will need to 
    // call the retain message on the pointer it gets back (see next)
    return pathArray;
}

@end

So in your caller code:

// create a tempory PathFactory (autorelease will make sure it is cleaned up when we
// are finished here)
PathFactory * newPathFactory = [[[PathFactory alloc] init] autorelease];
// grab the new array of Path objects and retain the memory. _newPathArray
// is a member of this class that you will need to release later.
_newPathArray = [[newPathFactory create] retain];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜