Skip to content

Instantly share code, notes, and snippets.

@netskink
Last active February 24, 2023 16:19
Show Gist options
  • Save netskink/ccc8b433cea6eac5fa4c930a447ea783 to your computer and use it in GitHub Desktop.
Save netskink/ccc8b433cea6eac5fa4c930a447ea783 to your computer and use it in GitHub Desktop.
try catch in swift and variable visibility
func mySimpleFileRead(path: String) throws -> UnitsData {
let fileName = (path as NSString).expandingTildeInPath
let url = URL(fileURLWithPath: fileName)
let data = try Data(contentsOf: url)
return try JSONDecoder().decode(UnitsData.self, from: data)
}
var unitsData2: UnitsData
do {
unitsData2 = try mySimpleFileRead(path: "~/progs/myxcode/ogrev1/ogrev1/ogrev1/units.json")
} catch {
throws JFDFileError.JSONDecodeFailed
}
print("=-+-+==.name is \(unitsData2.units[1].name)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment