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 Build a Simple iPhone App with Objective-C Creating a Data Model Creating a Data Collection

Data collection

Can we create NSArray and NSDictionary in .h file if we want to give global access. and if we are declaring these data collection in .m file then why we are doing it in under Particular method.

1 Answer

Karan, you have a very good questions. In most cases, dictionaries and arrays aren't used for global access, they are used to complete tasks within methods that are only stored within that method.

Here is an example:

-(float)addNumbersWithNumberOne:(float)numberOne numberTwo:(float)numberTwo {


    float answer = numberOne + numberTwo;

    return answer;
}
func addNumbers (numberOne: Float, numberTwo: Float) -> Float {

   var answer: Float = numberOne + numberTwo;

   return answer;
}

In the above examples, there is a data type, the answer, stored inside of the method to hold the sum of the two numbers. Since the methods caller of sender just cares about the return value, the answer float is completely irrelevant. As long as the code works, a programmer will not need to know the inner workings of a method.

If you have any questions, feel free to ask!