Skip to content

Instantly share code, notes, and snippets.

@daniloc
Created March 5, 2020 15:12
Show Gist options
  • Save daniloc/1664f913d0b966793da15de548626b33 to your computer and use it in GitHub Desktop.
Save daniloc/1664f913d0b966793da15de548626b33 to your computer and use it in GitHub Desktop.
Basic example of using string literals to load bundle resources
struct MarkdownFile: ExpressibleByStringLiteral {
let bundleName: String
let rawMarkdown: String?
init(stringLiteral: String) {
bundleName = stringLiteral
var loadedMarkdown: String? = nil
if let filepath = Bundle.main.path(forResource: bundleName, ofType: nil) {
do {
let loadedString = try String(contentsOfFile: filepath)
loadedMarkdown = loadedString
} catch {
print("Could not load string: \(error)")
}
} else {
print("Could not find file: \(bundleName)")
}
rawMarkdown = loadedMarkdown
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment