In one of my methods, I fetched and parsed a JSON
and placed it inside an NSArray
called jsonArray in -(void)method1
. I then copied the contents of that jsonArray to an NSMutableArray
called copiedJsonArray to be used on other methods. Problem is, copiedJsonArray crashes whenever I log its contents in the console from the other methods -(void)method2
but it logs fine in -(void)method1
.
How can I fix this?
In my header file:
@interface MainViewController : UIViewController
@property (nonatomic, retain) NSMutableArray *copiedJsonArray;
In my implementation file:
@synthesize copiedJsonArray;
- (void)viewDidLoad
{
[self method1];
}
- (void)method1
{
NSString *urlString = [NSString stringWithFormat:THE_URL];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *jsonString = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
NSDictionary *jsonDictonary = [jsonString JSONValue];
NSArray *jsonArray = [jsonDictonary valueForKeyPath:@"QUERY.DATA"];
self.copiedJsonArray = [[NSMutableArray alloc] initWithArray:jsonArray copyItems:YES];
NSLog(@"Copied JSON Array in Method 1: %@", self.copiedJsonArray);
[self method2];
}
- (void)method2
{
NSLog(@"Copied JSON Array in Method 2: %@", self.copiedJsonArray);
}
I also tried doing this too but it does the same error:
copiedJsonArray = [jsonArray mutableCopy];
I also tried implementing NSCopy
but fails too:
@interface MainViewController : UIViewController <NSCopying>
{
NSMutableArray *copiedJsonArray;
}
I'm doing this so that I can do a loop in my copiedJsonArray without fetching its contents from JSON again and again when the user taps on my UISegmentedControl
.