Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
dictionary
values
keys
communitycreator

How to get the values of a dictionary in Swift?

Theodore Kelechukwu Onyejiaku

Overview

A dictionary is made up of key-value pairs. The values property in Swift only returns the dictionary’s values without returning the keys.

Syntax

dictionary.values

Return value

The value returned is a collection containing the values of the Dictionary instance dictionary.

Code

// create a dictionary
var phones = [
  "S" : "Samsung",
  "I" : "Iphone",
  "G" : "Gionee",
  "T" : "Tecno"
]

// get only the values of the dictionary
print(phones.values)
Get all the values of a dictionary in Swift

Explanation

  • Line 2–6: We create a dictionary called phones and populate it with some elements.
  • Line 10: We get the dictionary’s values using the values property. The values are then printed to the console.

RELATED TAGS

swift
dictionary
values
keys
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring