Last active
October 19, 2016 04:42
-
-
Save alexpyoung/e94e29e5df4a20edbc78390031e1d5a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
final class JSONLoader { | |
static func path(forResource name: String) throws -> String { | |
let bundle = NSBundle(forClass: JSONLoader.self) | |
guard let path = bundle.pathForResource(name, ofType: "json") else { | |
// TODO | |
throw | |
} | |
return path | |
} | |
static func dictFromResource(withName name: String) throws -> [String:AnyObject] { | |
let path = try self.path(forResource: name) | |
let data = NSData.dataWithContentsOfMappedFile(path) | |
guard let object = NSJSONSerialization.JSONObjectWithData(data, options: .mutableContainers), | |
let dict = object as? [String:AnyObject] | |
else { | |
// TODO | |
throw | |
} | |
return dict | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment