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

Steven Rayzman
2,423 PointsMaking a array available to all files in xcode
How do I make an array available to all files in Xcode?
Also, how do I then check if that arrray contains a certain object?
3 Answers

Stone Preston
42,016 Pointsyou could add your array as a property in your appDelegate.h file. then you could access that array property in any viewController by first accessing the app delegate, then accessing the property of that app delegate (be sure to include the appDelegate header in your viewController) like so:
AppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
NSArray *myArray = myAppDelegate.theArray;
as far as determining whether or not the array contains a certain object you can use the [NSArray containsObject:] method

Holger Liesegang
50,595 Points2 minutes, Stone just 2 minutes :-)

Steven Rayzman
2,423 PointsSorry for the trouble, but can you step me through it more. Should I include the app delegate in the header like so
import "MIAppDelegate.h", and
then add the array as a property in MIAppDelegate.h. Then in the file I would import MIAppDelagate.h into i would put
AppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate]; NSArray *myArray = appDelegate.theArray;

Stone Preston
42,016 Pointsyes thats correct. thats exactly what you would need to do.

Steven Rayzman
2,423 PointsWould I include the code you posted in the implementation file.

Stone Preston
42,016 Pointsyep. you could use that whereever you needed to access that array. alternatively you could create a property in the view controllers you needed access to it in and set it in viewDidLoad and only have to worry about it once instead of using that code each and every time you needed it

Steven Rayzman
2,423 PointsCan you tell me how to use the code you posted. Whenever I post it in the viewDidLoad in the implementation I receive two errors
- Use of undeclared identifier 'myAppDelegate'
- Use of undeclared identifier 'AppDelegate'
Again, thank you for all your help

Stone Preston
42,016 Pointsyou will have to change the class name of the app delegate depending on what your app delegate is named. it could be SRAppDelegate or something in which case oyu need to use
SRAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
NSArray *myArray = myAppDelegate.theArray;

Steven Rayzman
2,423 PointsThank you that helped so much. But now I have another problem. I need the access the 'myArray' outside of the viewDidLoad method in the implementation, so that it could be used by the entire file. How would I go about doing that.

Stone Preston
42,016 PointsI mentioned earlier that "you could create a property in the view controllers you needed access to it in and set it in viewDidLoad and only have to worry about it once instead of using that code each and every time you needed it". so just add an NSArray property to your view controller and set that in view did load using something like:
SRAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
self.myArrayProperty = myAppDelegate.theArray;
then you can access throughout your controller using self.myArrayProperty or whatever you named it

Steven Rayzman
2,423 PointsThank you so much. You have made my day. Is there anyway to contact you through Treehouse so that if I have another question you would see. This is because you are the most helpful, kind, and reply fast.

Stone Preston
42,016 Pointsyeah you can just create a forum post and tag me in it by typing @ followed by my name (like on twitter). I can tag you like this: Steven Rayzman

Steven Rayzman
2,423 PointsSo this is my code ```PFUser *newUser=[PFUser user]; newUser.username=username; newUser.password=password; newUser.email=email;
if ([self.facultyField.text intValue]!=73) { nil; } else{ [self.facultyList addObject:newUser.username]; } [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (error) { UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertView show]; } else { [self.navigationController popToRootViewControllerAnimated:YES]; } }];
The facultyField is where a faculty member would enter the code 73. They then would be added to the facultyList array which is sourced from the AppDelegate. However, whenever I attempt to check if the current user is part of the faculty list array, it always turns logs 'nil' even if the current user was made to be part of the facultyList array.

Holger Liesegang
50,595 PointsHi Steven,
1 "How do I make an array available to all files in Xcode?"
You might want to make a new class file (derived from NSObject) und define a NSArray/NSMutableArray property in the header file of your new class file. This way you can easily access your NSArray via this new class from every other file(class) in your project.
2 "how do I then check if that arrray contains a certain object?"
You can use e.g. NSArray's
- (BOOL)containsObject:(id)anObject
method for this. You can find many more in the "Finding Objects in an Array" section of the NSArray Class Reference.

Steven Rayzman
2,423 PointsThanks
Steven Rayzman
2,423 PointsSteven Rayzman
2,423 PointsAlright, thanks