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

Alloc/init in App Delegate

Hey Stone Preston Matthew Mascioni Kevin Lozandier ,

If I want to create variables that should be the same and accessible on all view controllers in the application, can I simply declare and alloc/init them once in the App Delegate? If so, do I alloc/init them in the didLaunchWithOptions method? And to incorporate them on each view controller, do I just import the App Delegate file on each view controller? Would it only work, if the variables are read-only or can they also be changed?

also: concerning methods, when creating helper methods in the .m file, do i also have to declare them first? So far I haven't and just implemented them and on the test devices it worked fine, but is a substantial reason I should? Does Apple maybe disallow this or is it simply better coding practice? ;)

Thanks a lot!! :)

Cheers, Pascal

2 Answers

Hey Pascal,

I can answer one of those questions :) When you're creating helper methods in the .m file, without declaring them in the .h file, you're creating private methods of some sort. (Objective-C doesn't actually have private methods, a great StackOverflow thread on it is here)

What does this mean?

Say I had an app that fetches a list of elements from the periodic table and displays them. I have a ViewController, and I also have a class called Elements, which contains methods for getting elements.

I define a method in Elements in its .h file like this:

- (NSArray *)fetchAllElements;

Now, in my ViewController, I can initialize the Elements object called 'elementHelper' (don't make fun of my naming skills) and call the method:

NSArray *elements = [elementHelper fetchAllElements];

You should notice that the fetchAllElements method name auto-completes by Xcode if you defined it in the .h file.

Cool, right? Let's flip back to Elements.h and Elements.m. In our .m file, we have - (NSArray *)fetchAllElements re-written with its implementation. Let's say that within this method, before we return back the array of elements, we want to sort them by atomic number.

Instead of cluttering up our fetchAllElements method, we'll just create a new helper method to sort the elements. But, do we declare this method in the .h file? Our View Controller will never call this method-- all it wants is the returned elements array so it can display the contents. Thus, it's okay to declare this method in the .m file and use it throughout there.

However, notice that if you tried to call this helper method to sort the array outside the Elements class, Xcode won't auto-complete it and you might get errors that prevent you from compiling.

So, I suppose this is a way to keep your header files clean from methods that will only be called within the class' implementation. There's probably a more technical reason behind it I'm totally not aware of. Seems to me you're doing things right.

Hope this helps!

Wow, thanks for the detailed answer, Matthew! :D Have a nice weekend!