PDA

View Full Version : NSArray loses data




Jeremy1026
Jun 26, 2009, 10:52 PM
I have an NSArray declared in my .h, given a property, and synthesized. I put objects into the array, then log the count (51). I later log the count and its 0. Why am I losing my array data?


Declaring the array data:
NSString *FeedURL = @"...aURL...";
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:FeedURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
NSString *theString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
tempArray = [theString componentsSeparatedByString:@"<br>"];
if (err == nil) {
for (int i = 0; i < [tempArray count]; i++) {
NSLog(@"%@",[tempArray objectAtIndex:i]);
}
NSLog(@"Connected For SHIZZLE!!!!");
}
else {
NSLog(@"AHH, oops?");
tempArray = [NSArray arrayWithObjects:@"Internet connection required", nil];
}
NSLog(@"temp count - %i",[tempArray count]); //returns 51

Logging the array data down the road
- (void) changeView {
if (theIndexPath == 4) {
[window addSubview:twitterController.view];
NSLog(@"temp count - %i",[tempArray count]);//returns 0
}
}



Jeremy1026
Jun 26, 2009, 11:18 PM
Solved by member of another board.

I think you forgot to retain the tempArray.

If you declared a property 'tempArray' with the 'retain' attribute, you need to call - [self setTempArray] by using:


self.tempArray = ....