Maps (aka Associative Arrays)
This page is under construction. Please come back later.
#!/usr/bin/env swift; import Foundation import Utils // Begin Main 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)
Output
$ swiftc Maps1.swift -I . -L . -lUtils
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lUtils
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#!/usr/bin/env swift; import Foundation import Utils // Begin Main 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)
Output
$ swiftc Maps2.swift -I . -L . -lUtils
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lUtils
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]