Last active
February 26, 2016 06:13
-
-
Save MaddTheSane/23a9d4a5be682370b524 to your computer and use it in GitHub Desktop.
Bug hunting for Swift Reddit
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
// TODO: Write some awesome Swift code, or import libraries like "Foundation", | |
// "Dispatch", or "Glibc" | |
import Foundation | |
class ExampleClass { | |
var Id: Int | |
var Name: String | |
init(id: Int, name: String){ | |
Id = id | |
Name = name | |
} | |
} | |
//Save Data: | |
func arrayToDict(array: [AnyObject]) -> [String:[[String:String]]] { | |
//map your array to a dict | |
//this example uses a dict to emulate a json object | |
//with an array of dictionaries i.e.: | |
// {ClassName : [{ | |
// classVariable: Value, | |
// classVariable: Value | |
// }, | |
// { | |
// classVariable: Value, | |
// classVariable: Value | |
// } | |
//]} | |
var dict = [String:[[String:String]]]() | |
var arrayOfDicts = [[String:String]]() | |
for ex in exampleArray { | |
var innerDict = [String:String]() | |
innerDict["Id"] = String(ex.Id) | |
innerDict["Name"] = String(ex.Name) | |
arrayOfDicts.append(innerDict) | |
} | |
dict["ExampleObjects"] = arrayOfDicts | |
return dict | |
} | |
func dictToJsonData(dict: [String:[[String:String]]]) -> NSData? { | |
//first convert the dictionary to and NSDictionary type | |
let nsDict = dict as NSDictionary | |
//then we attempt to serilize the data into an NSDataObject | |
if let jsonData = try? NSJSONSerialization.dataWithJSONObject(nsDict, options: .PrettyPrinted) { | |
return jsonData | |
} else { | |
return nil | |
} | |
} | |
func writeToFile(data: NSData, filename: String) { | |
let path = getPathToDeveloperForFile(filename) | |
let fileManager = NSFileManager.defaultManager() | |
fileManager.createFileAtPath(path, contents: data, attributes: nil) | |
} | |
func getPathToDeveloperForFile(filename: String) -> String { | |
let basePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DeveloperDirectory, .UserDomainMask, true)[0] | |
return "\(basePath)/filename.json" | |
} | |
//Load Data: | |
func getNSData(filename: String) -> NSData? { | |
let fileManager = NSFileManager.defaultManager() | |
let path = getPathToDeveloperForFile(filename) | |
if let jsonData = fileManager.contentsAtPath(path) { | |
return jsonData | |
} else { | |
return nil | |
} | |
} | |
func dictFromNSData(data: NSData) -> NSDictionary? { | |
if let nsDict = try? NSJSONSerialization.JSONObjectWithData(data,options: .MutableContainers) as? NSDictionary { | |
return nsDict | |
} else { | |
return nil | |
} | |
} | |
func arrayFromDict(dictionary: NSDictionary) -> [ExampleClass] { | |
var array = [ExampleClass]() | |
if let example = dictionary["ExampleObjects"]! as? [[String:String]] { | |
for object in example { | |
if let id = object["Id"], name = object["Name"], idNum = Int(id) { | |
array.append(ExampleClass(id: idNum, name: name)) | |
} | |
} | |
} | |
return array | |
} | |
//exampleImplementation | |
var exampleArray = [ExampleClass]() | |
for i in 1...10 { | |
exampleArray.append(ExampleClass(id: i, name: "example \(i)")) | |
} | |
func saveArray() { | |
let dict = arrayToDict(exampleArray) | |
if let data = dictToJsonData(dict) { | |
writeToFile(data, filename: "example") | |
} | |
} | |
var exampleLoadedArray = [ExampleClass]() | |
func loadArray() { | |
if let data = getNSData("example"), dict = dictFromNSData(data) { | |
exampleLoadedArray = arrayFromDict(dict) | |
} | |
} | |
print("Example Array:") | |
for item in exampleArray { | |
print("\(item.Id): \(item.Name)") | |
} | |
saveArray() | |
loadArray() | |
print("exampleLoadedArray") | |
for item in exampleLoadedArray { | |
print("\(item.Id): \(item.Name)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment