"Build a Selfie App with Swift 2" was retired on May 31, 2020.

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

how to compare operands of type Any and type String in swift

Hi there, I was trying to sort an array of dictionaries where dictionary is of type [String:Any]

now I if I am trying to do following: var x=array[index]["Key"]

If x == true{ //do some thing }

But this gives me error

error: binary operator '==' cannot be applied to operands of type 'Any?' and 'String'

Please help me I am stuck :(

Thanks Anuradha.

1 Answer

Got it :) typecast the value of type "Any" to "Bool" as follows:

var x=array[index]["Key"] as? Bool

Its working. I can sort my array of Dictionary now.

Please let me know if I have done it correctly or is there any other way to do this.

Thanks, Anuradha.