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 Soccer League Coordinator in Swift

Jack Colosky
PLUS
Jack Colosky
Courses Plus Student 2,263 Points

Getting error : Value of type 'string' does not conform to expected dictionary value type 'AnyObject

When I create my dictionary and define the type, I get this error : Value of type 'string' does not conform to expected dictionary value type 'AnyObject.

let joeSmith: [String : AnyObject] = ["Name":"Joe Smith", "Experience": true, "Parent Guardian": "Mr. & Mrs. Smith"] let jillTanner:[String : AnyObject] = ["Name" : "Jill Tanner","Experience": true, "Parent Guardian": "Clara Tanner"]

please help,

Thanks,

Jack Colosky

2 Answers

Nathan F.
Nathan F.
30,773 Points

Hi Jack,

You can do some reading about the differences between Any and AnyObject in Swift at the link provided. This was also the approach I was going to take for this project, but I decided to keep things simple and work around everything as strings.

In any case, the short answer is that you should use [String: Any] not [String: AnyObject]. If I understand how types work in Swift.... String is not a class. It's a value type, or a Struct. AnyObject only works with classes, like Objective-C's NSString. Any will allow any value at all, not just classes.

Jack Colosky
Jack Colosky
Courses Plus Student 2,263 Points

Thank you sm! I also had one other question if you wouldn't mind.

I'm getting this error : Value of type '[String, Any]' has no member 'append'

// All the players have there own dictonary to store their name, height, soccer experience, and guardians name

let jSmith : [String : Any] = ["Name" : "Joe Smith", "Height" : 42, "Soccer Expo" : true, "Guardian" : "Jim and Jan Smith"] let jTanner : [String : Any] = ["Name" : "Jill Tanner", "Height" : 36, "Soccer Expo" : true, "Guardian" : "Clara Tanner"] let bBon : [String : Any] = ["Name" : "Bill Bon", "Height" : 43, "Soccer Expo" : true, "Guardian" : "Sara and Jenny Bon"] let eGordan : [String : Any] = ["Name" : "Eva Gordan", "Height" : 45, "Soccer Expo" : false, "Guardian" : "Wendy and Mike Gordan"] let mGill : [String : Any] = ["Name" : "Matt Gill", "Height" : 40, "Soccer Expo" : false, "Guardian" : "Charles and Sylvia Gill"] let kStein : [String : Any] = ["Name" : "Kimmy Stein", "Height" : 41, "Soccer Expo" : false, "Guardain" : "Bill and Hillary Stein"]

// An array to store all the dictonaries

let playerRoster = [jSmith, jTanner, bBon, eGordan, mGill, kStein]

//teams

let sharks : [String : Any] = [:] let dragons : [String : Any] = [:] let raptors : [String : Any] = [:]

//The types of players

let experPlayers : [String : AnyObject] = [:] let unexperPlayers : [String : AnyObject] = [:]

for player in playerRoster { if player["Soccer Expo"] as! Bool == true { experPlayers.append(player) } else { unexperPlayers.append(player) } }

sorry for all the questions,

thanks,

Jack Colosky

Nathan F.
Nathan F.
30,773 Points

Hey there, sure I can give you a pointer. Actually the error is telling you exactly what you need to know. ;)

You are getting Value of type '[String, Any]' has no member 'append' because you are calling the append() method on a dictionary, not an array. Check how you've defined your experPlayers and unexperPlayers constants:

experPlayers : [String : AnyObject] <-- this is a dictionary

What you want is an Array of dictionaries, so add another set of braces: [[String : AnyObject]]

One last hint: you've defined your experPlayers and unexperPlayers as constants using let. This means they won't and can't change. Are you sure that's what you want? :)

Best of luck!