Created
March 5, 2020 15:12
-
-
Save daniloc/1664f913d0b966793da15de548626b33 to your computer and use it in GitHub Desktop.
Basic example of using string literals to load bundle resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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