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 Weather App with Swift Simple Data Structures Modeling the Current Weather

peter keves
peter keves
6,854 Points

AnyObject

What does AnyObject exactly mean ? and why is it used here ?

2 Answers

James Hoffman
James Hoffman
6,354 Points

[String : AnyObject] is the dictionary type. It has String keys and returns various types. Hope this helps Jhoan!

Hey Peter

The following is directly from the documentation, so can't take credit The Swift Programming Language

Type Casting for Any and AnyObject

Swift provides two special type aliases for working with non-specific types:

AnyObject can represent an instance of any class type. Any can represent an instance of any type at all, including function types. NOTE

Use Any and AnyObject only when you explicitly need the behavior and capabilities they provide. It is always better to be specific about the types you expect to work with in your code.

AnyObject

When working with Cocoa APIs, it is common to receive an array with a type of [AnyObject], or “an array of values of any object type”. This is because Objective-C does not have explicitly typed arrays. However, you can often be confident about the type of objects contained in such an array just from the information you know about the API that provided the array.

In these situations, you can use the forced version of the type cast operator (as) to downcast each item in the array to a more specific class type than AnyObject, without the need for optional unwrapping.

Jhoan Arango
Jhoan Arango
14,575 Points

Hey Rasmus, perhaps you may be able to help me understand this part. [String: AnyObject]

I am not quite sure what it means. Is this some sort of type casting ? Pasan refers it to “String to AnyObject type.