How to add elements to a Dictionary in Swift?
Hi everyone! I just started learning Swift and ran into a problem working with dictionaries. In other languages, like Python or JavaScript, I easily added new key-value pairs to a dictionary. But in Swift, something went wrong.\n\nI have a simple dictionary that stores student names and their grades:\n\n```swift\nvar studentGrades = ["Ivan": 5, "Maria": 4]\n```\n\nNow I want to add a new student — Peter with a grade of 3. I tried doing this:\n\n```swift\nstudentGrades.append("Peter": 3)\n```\n\nBut Xcode gives an error: "Value of type '[String : Int]' has no member 'append'". I also tried the `add` method:\n\n```swift\nstudentGrades.add("Peter": 3)\n```\n\nIt doesn't work either. Please tell me how to correctly add elements to a dictionary in Swift? I heard that in older versions of Swift
No answers yet.
Log in to leave an answer
Blogs