-
-
Save KrisYu/9829cd2721966a783ee5c92dc6826363 to your computer and use it in GitHub Desktop.
RW to file in swift
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
// | |
// RW.swift | |
// Web | |
// | |
// Created by Mike Chirico on 10/15/15. | |
// Copyright © 2015 Mike Chirico. All rights reserved. | |
// | |
import UIKit | |
/* | |
Example way to use the class below | |
let rw = RW() | |
var mDict:[String:String] = ["one":"1","two":"2"] | |
rw.write("test0",mDict: mDict) | |
mDict["one"]="g" | |
rw.read("test0",mDict: &mDict) | |
print(mDict) | |
or | |
let rw = RW() | |
var mDict = ["one":"1","two":"2","three":3,"more":["stuff":3]] as NSObject | |
rw.write("test0",mDict: mDict) | |
var r = mDict as! [String:NSObject] | |
r["one"]="e" | |
print("Change \(r)") | |
rw.read("test0",mDict: &mDict) | |
print(mDict) | |
Actually, you can get much more complicated | |
*/ | |
// class A:NSObject, NSCoding { | |
// var s:String="" | |
// var mDictS:[String:[Int]]=[:] | |
// var mDictN:[String:[NSObject]]=[:] | |
// | |
// override init() { | |
// | |
// } | |
// | |
// @objc required init(coder aDecoder: NSCoder) { | |
// s = aDecoder.decodeObjectForKey("s") as! String | |
// mDictS = aDecoder.decodeObjectForKey("mDictS") as! [String:[Int]] | |
// mDictN = aDecoder.decodeObjectForKey("mDictN") as! [String:[NSObject]] | |
// | |
// } | |
// | |
// @objc func encodeWithCoder(aCoder: NSCoder) { | |
// aCoder.encodeObject(s, forKey: "s") | |
// aCoder.encodeObject(mDictS, forKey: "mDictS") | |
// aCoder.encodeObject(mDictN, forKey: "mDictN") | |
// } | |
// | |
// func pr() -> String { | |
// return s | |
// } | |
// } | |
// | |
// | |
// let a = A() | |
// a.s="more" | |
// print(a.pr()) | |
// | |
// | |
// let rw = RW() | |
// var mDict = ["one":"1","two":"2","three":3,"more":["stuff":3],"a":a] as NSObject | |
// | |
// rw.write("test0",mDict: mDict) | |
// var r = mDict as! [String:NSObject] | |
// r["one"]="e" | |
// print("Change \(r)") | |
// | |
// rw.read("test0",mDict: &mDict) | |
// print(mDict) | |
// r = mDict as! [String:NSObject] | |
// if let result = r["a"] as! A? { | |
// print(result.pr()) | |
// } | |
// | |
// var tDict = ["one":"1"] | |
// rw.write("test2",mDict: tDict) | |
// tDict["one"]="3" | |
// rw.read("test2", mDict: &tDict) | |
// print(tDict) | |
// | |
// | |
class RW { | |
var documentsURL: NSURL? | |
var fileURL:NSURL? | |
func setFileURL(file: String) { | |
documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, | |
inDomains: .UserDomainMask)[0] | |
fileURL = documentsURL!.URLByAppendingPathComponent(file) | |
} | |
func write(file: String, mDict: [String:String]) { | |
setFileURL(file) | |
let data : NSData = NSKeyedArchiver.archivedDataWithRootObject(mDict) | |
if let fileAssigend = fileURL { | |
data.writeToURL(fileAssigend, atomically: true) | |
} | |
} | |
func read(file: String,inout mDict: [String:String]) { | |
setFileURL(file) | |
var data:NSData? | |
if let url = fileURL { | |
data = NSData(contentsOfURL: url) | |
if let result = data { | |
mDict = NSKeyedUnarchiver.unarchiveObjectWithData(result) as! [String:String] | |
} | |
} | |
} | |
// Below with NSObject | |
func write(file: String, mDict: NSObject) { | |
setFileURL(file) | |
let data : NSData = NSKeyedArchiver.archivedDataWithRootObject(mDict) | |
if let fileAssigend = fileURL { | |
data.writeToURL(fileAssigend, atomically: true) | |
} | |
} | |
func read(file: String,inout mDict: NSObject) { | |
setFileURL(file) | |
var data:NSData? | |
if let url = fileURL { | |
data = NSData(contentsOfURL: url) | |
if let result = data { | |
mDict = NSKeyedUnarchiver.unarchiveObjectWithData(result) as! NSObject | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment