开发者

Obj-c: returning NSArray from method - objects out of scope

My question is very similar to this question but with a few differences.

I have a class that takes in XML int the form of an NSString, parses out some objects and returns them in an NSArray somewhat like this:

//Parser.m
+(NSArray *)parseMessagesFromXml:(NSString *)xml
{
   NSMutableArray *messages = [[NSMutableArray alloc] init];

   //do some work parsing the xml
   //for each <row>
   //   Message *m = makeMessageFromRow(row);

   return [messages autorelease];
}

Then in my view controller class I declare an NSArray:

//MyViewController.h
NSArray *messages;
@property (nonatomic, retain) NSArray *messages;

and assign it using the above method:

//MyViewController.m
messages = [Parser parseMessageFromXml:xml];
[[self tableView] reloadData];

So here comes the problem: when i assign the array to messages it has elements in it, but they are all "out of scope." I have debugged the problem and I know that the parsing method is correctly creating the objects and adding them to the NSMutableArray before returning it. When I try to access the objects in messages my app crashes and the debugger says EXC_BAD_ACCESS. What is more peculiar is this: if i store the array into a local variable it works just fine:

NSArray *temp = [Pa开发者_JAVA百科rser parseMessageFromXml:xml]; 
//temp has all the right objects and they are in scope

messages = temp;
//messages has the objects, but cannot access them (they are out of scope).

It is as if I can legally view them in a local scope, but i cannot assign them to a member variable. I have even tried iterating over the returned array, adding each one to messages individually, but the result is the same: they are out of scope. I am totally clueless on this one.

What is it about messages as a member variable that doesn't allow it to hold these objects?


The problem is that the array is being released. When you call autorelease in parseMessagesFromXml:, you tell the array that it should be released sometime in the future. This is happening before the table reloads its data. You need to retain the array again to prevent it from being released. In this case, it is as simple as using the accessor methods to set your property instead of setting the instance variable directly.

//MyViewController.m
self.messages = [Parser parseMessageFromXml:xml];
[[self tableView] reloadData];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜