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

Saleh Bubishate
Saleh Bubishate
1,613 Points

can somebody help me,,, how can I use the key of dictionaries to assign values for instances?

I want to use the structure to assign instances for urologyResidents array items, but I had error when I want to use doctorBleep and doctorId dictionaries to declare id and bleep properties. this is the code (sorry I don't know how to do it)

struct ResidentInfo {
    var name : String
    var level : String
    var id : [Int]
    var bleep : [Int]
}

var urologyResidents = ["Hussam", "Ahmed", "Alhassan:", "Anas", "Saleh"]

let urologyLevels = [ "R1", "R2", "R3", "R4", "R5"]

var doctorBleep = [
    "Hussam" : 1517,
    "Ahmed": 1784,
    "Alhasaan": 1417,
    "Anas": 1508,
    "Saleh" : 1612
]

var doctorId = [
    "Hussam" : 14977,
    "Ahmed": 15510,
    "Alhasaan": 52291,
    "Anas": 52305,
    "Saleh" : 51542
]


let Hussam = ResidentInfo(name: urologyResidents[0], level: urologyLevels[0], id: doctorId["Hussam"], bleep:  doctorBleep["Hussam"])

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello,

You can do some changes to your code to make it work, from the dictionary you want to pass an ID, and it should return a Int, therefore in your struct you should change the store property to be of type "Int" instead of "[Int]"

struct ResidentInfo {
    var name : String
    var level : String
    var id : Int          // Instead of [Int]
    var bleep : Int.   // Instead of [Int]
}

var urologyResidents = ["Hussam", "Ahmed", "Alhassan:", "Anas", "Saleh"]

let urologyLevels = [ "R1", "R2", "R3", "R4", "R5"]

var doctorBleep = [
    "Hussam" : 1517,
    "Ahmed": 1784,
    "Alhasaan": 1417,
    "Anas": 1508,
    "Saleh" : 1612
]

var doctorId = [
    "Hussam" : 14977,
    "Ahmed": 15510,
    "Alhasaan": 52291,
    "Anas": 52305,
    "Saleh" : 51542
]


let Hussam = ResidentInfo(name: urologyResidents[0], level: urologyLevels[0], id: doctorId["Hussam"]!, bleep:  doctorBleep["Hussam"]!)
Saleh Bubishate
Saleh Bubishate
1,613 Points

thanks a lot ,,

can use automated function,,, like switch OR for in to assign ID and Bleep for each resident, please ?