Pure Programmer
Blue Matrix


Cluster Map

Maps (aka Associative Arrays)

L1

This page is under construction. Please come back later.

Maps1.swift
#!/usr/bin/env swift;
import Foundation
import Utils

func main() -> Void {
	var dictionary:[String: String] = [:]

	print("dictionary size: " + String(dictionary.count))
	print("dictionary is " + (dictionary.isEmpty ? "empty" : "not empty"))
	dictionary["apple"] = "red"
	dictionary["banana"] = "yellow"
	dictionary["cantaloupe"] = "orange"
	dictionary["dragonfruit"] = "red"
	dictionary["elderberry"] = "purple"

	print("dictionary size: " + String(dictionary.count))
	print("dictionary is " + (dictionary.isEmpty ? "empty" : "not empty"))

	print("bananas are " + dictionary["banana"])

	var x:String = "dragonfruit"
	if dictionary.keys.contains(x) {
		print(x + " are " + dictionary[x])
	} else {
		print("Can't find " + x)
	}

	x = "fig"
	if dictionary.keys.contains(x) {
		print(x + " are " + dictionary[x])
	} else {
		print("Can't find " + x)
	}

	x = "elderberry"
	dictionary.removeValue(forKey: x)
	if dictionary.keys.contains(x) {
		print(x + " are " + dictionary[x])
	} else {
		print("Can't find " + x)
	}

	print(Utils.mapToString(dictionary))
	exit(EXIT_SUCCESS)
}
main()

Output
Maps1.swift:19:35: error: value of optional type 'String?' must be unwrapped to a value of type 'String' 17 | print("dictionary is " + (dictionary.isEmpty ? "empty" : "not empty")) 18 | 19 | print("bananas are " + dictionary["banana"]) | |- error: value of optional type 'String?' must be unwrapped to a value of type 'String' | |- note: coalesce using '??' to provide a default when the optional value contains 'nil' | `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil' 20 | 21 | var x:String = "dragonfruit" Maps1.swift:23:33: error: value of optional type 'String?' must be unwrapped to a value of type 'String' 21 | var x:String = "dragonfruit" 22 | if dictionary.keys.contains(x) { 23 | print(x + " are " + dictionary[x]) | |- error: value of optional type 'String?' must be unwrapped to a value of type 'String' | |- note: coalesce using '??' to provide a default when the optional value contains 'nil' | `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil' 24 | } else { 25 | print("Can't find " + x) Maps1.swift:30:33: error: value of optional type 'String?' must be unwrapped to a value of type 'String' 28 | x = "fig" 29 | if dictionary.keys.contains(x) { 30 | print(x + " are " + dictionary[x]) | |- error: value of optional type 'String?' must be unwrapped to a value of type 'String' | |- note: coalesce using '??' to provide a default when the optional value contains 'nil' | `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil' 31 | } else { 32 | print("Can't find " + x) Maps1.swift:38:33: error: value of optional type 'String?' must be unwrapped to a value of type 'String' 36 | dictionary.removeValue(forKey: x) 37 | if dictionary.keys.contains(x) { 38 | print(x + " are " + dictionary[x]) | |- error: value of optional type 'String?' must be unwrapped to a value of type 'String' | |- note: coalesce using '??' to provide a default when the optional value contains 'nil' | `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil' 39 | } else { 40 | print("Can't find " + x)
Maps2.swift
#!/usr/bin/env swift;
import Foundation
import Utils

func main() -> Void {
	var planetDiameters:[String: Int] = [
		"Mercury": 4879,
		"Venus": 12103,
		"Earth": 12756,
		"Mars": 6794,
		"Jupiter": 142985,
		"Saturn": 120534,
		"Uranus": 51115,
		"Neptune": 49534,
		"Pluto": 2374,
		"Ceres": 946,
		"Eris": 2326,
		"Makemake": 1430
	]

	for planet in planetDiameters.keys {
		print(planet + " has a diameter of " + String(planetDiameters[planet]) + " km")
	}
	exit(EXIT_SUCCESS)
}
main()

Output
Maps2.swift:22:64: error: value of optional type 'Int?' must be unwrapped to a value of type 'Int' 20 | 21 | for planet in planetDiameters.keys { 22 | print(planet + " has a diameter of " + String(planetDiameters[planet]) + " km") | |- error: value of optional type 'Int?' must be unwrapped to a value of type 'Int' | |- note: coalesce using '??' to provide a default when the optional value contains 'nil' | `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil' 23 | } 24 | exit(EXIT_SUCCESS)
swift

Questions

Projects

More ★'s indicate higher difficulty level.

References