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 Objective-C Basics (Retired) Advanced Objective-C Dynamic Typing

Daniel Nochta
Daniel Nochta
1,377 Points

Dynamic Typing

I don't know if its just me but I really don't understand the whole dynamic typing logic. I mean through out the whole course I had some struggles but this one is a big one. Can someone please explain me this whole topic on some relevant tangible example of usage? Is dynamic typing really that relevant to know?

Thank you very much

Daniel

2 Answers

Yes it is relevant. Lets say we have an array. Inside this array we assume there are int values.

for(int value in myArray){
//do some addition/subtraction (basic math example)
}

This will work fine as long as it is actually ints in our array. But what if there are different types of objects in our array. e.g NSString, int, float, nsdictionary etc( could be anything) and we try to do some addition/subtraction? This would cause an exception/error. So basically he uses id which is generic. It could represent any type of object.

for(id value in myArray){
//check if id is of type int then do addition/subtraction
}

In his example he shows how in the NSlog the compiler is able to know what type of object it is.

Daniel Nochta
Daniel Nochta
1,377 Points

Thank you very much :)

I understand it:)