Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mike Lee
6,579 PointsUsing an array outside of a method.
How do I create an array inside of method,and then get it to retain all of its data outside of its method
11 Answers
Stone Preston
42,016 Pointsif you create the array inside the method, the array object is scoped to that method. So if you wanted to have an array thats accessible anywhere (inside that method and outside it) you could create a NSArray property or instance variable and use that
Stone Preston
42,016 Pointsif you added it as a property, you should access it using self.currentUsers, not just currentUsers. so try making that change
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
self.currentUsers=[NSArray arrayWithArray:objects];
NSLog(@"%@",self.currentUsers.description);
gymInfoDetailVC.gymUsers = self.currentUsers;
}];
}
Mike Lee
6,579 PointsI made the changes you suggested, then I tried to NSLog it outside of that block ,and my array still comes out nil.
Stone Preston
42,016 Pointsit could be the objects array is nil and something is wrong with your query. log that first and see what happens
NSLog(@"objects: %@", objects);
self.currentUsers=[NSArray arrayWithArray:objects];
NSLog(@"%@",self.currentUsers.description);
gymInfoDetailVC.gymUsers = self.currentUsers;
Mike Lee
6,579 PointsWhen i log it on the inside of the block , i get the data that queried for.
Stone Preston
42,016 Pointscan you post your property declaration?
Mike Lee
6,579 Points@property (strong,nonatomic) NSMutableArray * currentUsers;
Stone Preston
42,016 Pointshmm that looks fine too. Looks to me like everything should be working just fine. But its not. The only thing im not sure of is the fact you synthesized it. I got into iOS after you no longer had to do that so ive got not experience with it. Try removing the line where you synthesize it see its not necessary and see if that makes a difference
Mike Lee
6,579 PointsI took away the synthesize statement,but then the method doesnt recognize the declaration.
Mike Lee
6,579 PointsI took away the synthesize statement then the method doesn't recognize the declaration.
Stone Preston
42,016 Pointspost the synthesize statement. What version of xcode are you using by the way? Also after you took away the synthesize make sure you access the property using self.currentUsers
Mike Lee
6,579 Points@synthesize currentUsers; Xcode 4.6.3
Stone Preston
42,016 Pointsok yeah you shouldnt have to synthesize if you are using above 4.4. Hmm, double check you are using self.currentUsers to access the property after removing the synthesize. I really dont see why this isnt working for you. very strange. I was hoping the synthesize would be the issue and something was getting screwed up with the getters and setters, but apparently its something else
Stone Preston
42,016 Pointsmay i ask why you havent updated to 5 yet?
Mike Lee
6,579 PointsYeah I tried that too and still nothing. I havent updated yet because I still have a older MAC lol
Stone Preston
42,016 Pointsthe only thing I can think of is that somewhere else you are setting that array to nil.
Mike Lee
6,579 PointsIll take a closer look at things and let you know what I find.I really appreciate your time .
Derek Medlin
6,151 PointsOne thing i noticed is you're declaring an NSMutableArray but sending it NSArray. Still it shouldn't return nil. I don't know...
Stone Preston
42,016 PointsNSMutableArray inherits from NSArray, so thats a valid call
Derek Medlin
6,151 PointsTrue but it still throws warnings as incompatible pointer types, but none the less it still don't solve the problem :P
Stone Preston
42,016 Pointsyeah looks like the compiler shows a warning so he probably should change it to NSMutableArray, but it works correctly none the less.
Mike Lee
6,579 PointsHey guys, Ive been going at this relentlessly and I still cant get it to work .
Derek Medlin
6,151 Pointsdo you have a instance variable and a property named currentUsers? Are you possibly assigning self.currentUsers and NSLogging with the instance variable (without the "self" prefix)?
Mike Lee
6,579 Pointsi finally got it , i had to put all the code i was using into that code block that was performing asynchronously .
Stone Preston
42,016 Pointsahh. you can also prefix a variable with __block which makes the modifications made inside the block persist outside of it.
example: __block char localCharacter;
Mike Lee
6,579 PointsOkay I will give that a shot as well.
Mike Lee
6,579 PointsMike Lee
6,579 PointsI created an array property, synthesized it, and then used it inside a method,but when I try to log it on the outside to see if it retained its data,it returned null.
Stone Preston
42,016 PointsStone Preston
42,016 Pointsyou shouldnt have to synthesize it if you are using xcode 5 (and even xcode 4). thats done automatically for you. Can you post the code you used inside the method?
Mike Lee
6,579 PointsMike Lee
6,579 Points-(void)mapView:(MKMapView * )mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
GymInfoDetailViewController *gymInfoDetailVC =[self.storyboard instantiateViewControllerWithIdentifier:@"GymInfoDetailViewController"];
NSString *gymLabelText = view.annotation.title;[self.navigationController pushViewController:gymInfoDetailVC animated:YES]; [gymInfoDetailVC.gymLabel setText:gymLabelText];