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 do I write a function that gets a Dictionary?

Hi,

I am trying to write a function that gets a Dictionary as a parameter and then make to this Dictionary changes in my function.

But when I type :

func dictonaryTuple(Dictonary: Dictionary) -> Dictionary<Int,Double>

It dose't work and the Xcode says that there is a problem.

Can someone please help me ?

Thank you very much ! :)

1 Answer

without knowing the context, i would go for a struct...

Like so:

struct MyStruct {

    var valueOne: String
    var valueTwo: Int
    var valueThree: Double

    init(myDictionary: NSDictionary) {
        let newDictionary = myDictionary["dictionary"] as NSDictionary
        valueOne = newDictionary["keyOne"] as String
        valueTwo = newDictionary["keyTwo"] as Int
        valueThree = newDictionary["keyThree"] as Double
    }
}

YOUR example would normally be written like this:

func dictonaryTuple(Dictonary: NSDictionary) -> NSDictionary {

    // Your Code here

    return Dictonary
}

I hope this helps