Created
December 28, 2016 16:34
-
-
Save fbukevin/4e61afee9dd55aae09383eb2198a9e20 to your computer and use it in GitHub Desktop.
Load JSON file in Swift 3
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
func loadJSON(file: String, handler: (NSDictionary) -> Void){ | |
if let path = Bundle.main.path(forResource: file, ofType: "json") { | |
do { | |
let jsonData = try NSData(contentsOfFile: path, options: NSData.ReadingOptions.mappedIfSafe) | |
do { | |
let json: NSDictionary = try JSONSerialization.jsonObject(with: jsonData as Data | |
, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary | |
handler(json) | |
} catch { | |
print("Parse json failed") | |
} | |
} catch { | |
print("Load json failed") | |
} | |
} else { | |
print("\(file).json open failed") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment