Created
May 17, 2015 01:11
-
-
Save garmstro/594bbf846bd88fb0bd01 to your computer and use it in GitHub Desktop.
JSON File Reader
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
// | |
// JsonHandler.swift | |
// | |
// Created by Geoff Armstrong on 4/6/15. | |
// Copyright (c) 2015 Geoff Armstrong. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
/** | |
Class to handle the JSON file format of the peak listing | |
*/ | |
class JsonHandler: NSObject { | |
override init() { | |
super.init() | |
} | |
/** | |
Read data from a JSON file located in the main bundle | |
:param: fileName Name of the json file | |
:returns: NSDictionary if successful, nil otherwise | |
*/ | |
func jsonReadData(fileName: String) -> NSDictionary? { | |
var error: NSError? | |
var jsonArray: NSDictionary | |
var dir: String? | |
let dirs : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] | |
let bundle = NSBundle.mainBundle() | |
dir = bundle.resourcePath | |
if let path = dir?.stringByAppendingPathComponent(fileName) { | |
if var input = NSInputStream(fileAtPath: path) { | |
input.open() | |
if input.hasBytesAvailable { | |
jsonArray = NSJSONSerialization.JSONObjectWithStream(input, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary | |
input.close() | |
//Success! | |
return jsonArray | |
} | |
else { | |
input.close() | |
} | |
} | |
} | |
//If all else fails... | |
return nil | |
} | |
/** | |
Read names from a JSON file located in the main bundle | |
:param: fileName Name of the json file | |
:returns: Array of String names | |
*/ | |
func readNames(fileName: String) -> [String] { | |
//read JSON file | |
var dict = jsonReadData(fileName) | |
var name: String | |
var names: [String] = [] | |
// Read Objects from System File | |
if dict != nil { | |
if let objects: NSArray = dict?["objects"] as? NSArray { | |
for var i=0; i < objects.count; i++ { | |
if let objectDict: NSDictionary = objects[i] as? NSDictionary { | |
if let objectName = objectDict["name"] as? NSDictionary { | |
name = objectName as! String | |
names.append(name) | |
} | |
} | |
} | |
} | |
} | |
return names | |
} | |
/** | |
Read peak dictionary from a JSON file located in the main bundle | |
:param: fileName Name of the json file | |
:returns: Array of NSDictionary if successful, nil otherwise | |
*/ | |
func readObjects(fileName: String) -> [NSDictionary]? { | |
//read JSON file | |
var dict = jsonReadData(fileName) | |
var objects: [NSDictionary]? | |
// Read Objects from System File | |
if dict != nil { | |
objects = dict!["objects"] as? [NSDictionary] | |
if objects != nil { | |
//Success! | |
return objects | |
} | |
} | |
//If all else fails... | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment