开发者

how to preserve array outside of method in objective c

After a ASIFormDataRequest , i create a temporary NSMutableArray *resultArray from the JSON then add it to a defined NSMutablearray *myData

-(void)viewDidLoad{
    myData = [[NSMutableArray alloc] init];
    //request that calls gotInfo method
}

-(void)gotInfo:(ASIFormDataRequest *)request{
    NSString *responseString = [request responseString];
    NSMutableArray *resultArray = [responseString yajl_JSON];
    [myData addObject:resultArray];
}

-(IBAction)doSomethingWithData:(id)sender{
    //something with myData
}

but when i try to call myData from outside of the gotInfo: method, i get bad access errors and when i inspect myData outside of the method, it shows a kern_protection_failure. So i'm guessing that outside of the method, the resultArray is obviously released, but it's also released from myData since the object inside myData is sharing the same memory location? I also tried

-(void)gotInfo:(ASIFormDataRequest *)request{
    NSString *responseString = [request responseString];
    [myData addObject:[responseString yajl_JSON]];
}

How do I preserve myData??

in my header file:

#import <UIKit/UIKit.h>

@class ASIForm开发者_StackOverflowDataRequest;

@interface EventsTableController : UITableViewController <UITableViewDataSource>{
    NSMutableArray *myData;
}
-(void)gotInfo:(ASIFormDataRequest *)request;

UPDATE:

so in the gbd, the myData is allocated as 0x5e96560 so i did

po 0x5e96560

and then i get the EXC_BAD_ACCESS with the reason being KERN_PROTECTION_FAILURE at address: 0x00000009

but if i do

po [[0x5e96560 objectAtIndex:0] objectForKey:@"key"]

then i get the value! whyyyyyy?


@property(nonatomic,retain) NSMutableArray *myData 

and create the object

self.myData = [[NSMutableArray alloc] init];

and

// and i assume your resultArray is a mature NSMutableArray object
[self.myData addObject:resultArray];

The best way of using copy I can think of, is to always set NSString properties to "copy" instead of retain. That way you get more accurate readings from the Leaks instrument if you mess up and forget to release a string an object is holding onto. Other uses of copy need to be more carefully thought out.

NOTE : You are responsible to release myData after no use of that variable.


You dont really have any way to correctly access myData as you declare it as a member inside of EventsTableController, but you dont set the @property for it, and do not synthesize it either. By synthesizing it in your EventsTableController.m file you are telling xcode to generate the getter/setters you need to correctly touch myData, which is where your program seems to be failing. If you do this, this should solve your problem.

-Karoly


Except for the different name of your ivar (mienVar vs. myVar), I don't see a problem. Some other code must be releasing your ivar, or you are accessing it before viewDidLoad has the opportunity to actually create the array (I bet it is the latter).

I think you should put the code in viewDidLoad in your initialization method instead. Don't forget to release the array in dealloc.

You could, of course, also write your own myData getter method, doing lazy initialization, instead of creating it in the init method:

- (NSMutableArray *) myData
{
    if (!myData)
        myData = [[NSMutableArray alloc] init];
    return myData;
}

Note that now, you should access self.myData if you want to use it.


I think the NSString yajl_JSON category can return an array or a dictionary - you might need to inspect the type of the result array on the line below as it may be an NSDictionary:

NSMutableArray *resultArray = [responseString yajl_JSON];

IF you are treating it as an array when its a dictionary that might be causing your problems.

(relevant code from the NSObject+YAJL category below)

YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:options error:error];
id root = [document.root retain];
[document release];
return [root autorelease];

(and in YAJLDocument object)

@interface YAJLDocument : NSObject <YAJLParserDelegate> {
(id root_; // NSArray or NSDictionary
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜