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

iOS

Using 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

if 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

I 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.

you 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?

-(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];

PFQuery *query = [PFUser query];
[query whereKey:@"gymName" equalTo:view.annotation.title];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)

 {


     currentUsers=[NSArray arrayWithArray:objects];
    NSLog(@"%@",currentUsers.description);

     gymInfoDetailVC.gymUsers = currentUsers;


 }];




}

if 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;


 }];




}

I made the changes you suggested, then I tried to NSLog it outside of that block ,and my array still comes out nil.

it 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;

When i log it on the inside of the block , i get the data that queried for.

can you post your property declaration?

@property (strong,nonatomic) NSMutableArray * currentUsers;

hmm 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

I took away the synthesize statement,but then the method doesnt recognize the declaration.

I took away the synthesize statement then the method doesn't recognize the declaration.

post 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

@synthesize currentUsers; Xcode 4.6.3

ok 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

may i ask why you havent updated to 5 yet?

Yeah I tried that too and still nothing. I havent updated yet because I still have a older MAC lol

the only thing I can think of is that somewhere else you are setting that array to nil.

Ill take a closer look at things and let you know what I find.I really appreciate your time .

One thing i noticed is you're declaring an NSMutableArray but sending it NSArray. Still it shouldn't return nil. I don't know...

NSMutableArray inherits from NSArray, so thats a valid call

True but it still throws warnings as incompatible pointer types, but none the less it still don't solve the problem :P

yeah looks like the compiler shows a warning so he probably should change it to NSMutableArray, but it works correctly none the less.

Hey guys, Ive been going at this relentlessly and I still cant get it to work .

do 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)?

i finally got it , i had to put all the code i was using into that code block that was performing asynchronously .

ahh. you can also prefix a variable with __block which makes the modifications made inside the block persist outside of it.

example: __block char localCharacter;

Okay I will give that a shot as well.