关于swift中的plist文件的写入用来存储信息,例如登录的时候出入信息,这里给一个完成的代码
2017-04-05 16:46
495 查看
http://www.jianshu.com/u/afebe9d5dc84
https://github.com/13670242169/QYPSQLFMDBManager.git
https://github.com/13670242169/QYPWEIBO.git
import UIKit
let BedroomFloorKey = “BedroomFloor”
let BedroomWallKey = “BedroomWall”
class ViewController: UIViewController {
}
https://github.com/13670242169/QYPSQLFMDBManager.git
https://github.com/13670242169/QYPWEIBO.git
import UIKit
let BedroomFloorKey = “BedroomFloor”
let BedroomWallKey = “BedroomWall”
class ViewController: UIViewController {
var bedroomFloorID: AnyObject = 103 as AnyObject var bedroomWallID: AnyObject = 103 as AnyObject override func viewDidLoad() { super.viewDidLoad() loadGameData() } @IBAction func saveButtonTapped(sender: AnyObject) { bedroomFloorID = 9999 as AnyObject saveGameData() } func loadGameData() { // getting path to GameData.plist let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray let documentsDirectory = paths[0] as! NSString let path = documentsDirectory.appendingPathComponent("GameData.plist") let fileManager = FileManager.default //check if file exists if(!fileManager.fileExists(atPath: path)) { // If it doesn't, copy it from the default file in the Bundle if let bundlePath = Bundle.main.path(forResource: "GameData", ofType: "plist") { let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath) print("Bundle GameData.plist file is --> \(String(describing: resultDictionary?.description))") try! fileManager.copyItem(atPath: bundlePath, toPath: path) print("copy") } else { print("GameData.plist not found. Please, make sure it is part of the bundle.") } } else { print("GameData.plist already exits at path.") // use this to delete file from documents directory //fileManager.removeItemAtPath(path, error: nil) } let resultDictionary = NSMutableDictionary(contentsOfFile: path) print("Loaded GameData.plist file is --> \(String(describing: resultDictionary?.description))") let myDict = NSDictionary(contentsOfFile: path) if let dict = myDict { //loading values bedroomFloorID = dict.object(forKey: BedroomFloorKey)! as AnyObject print(bedroomFloorID) bedroomWallID = dict.object(forKey: BedroomWallKey)! as AnyObject //... } else { print("WARNING: Couldn't create dictionary from GameData.plist! Default values will be used!") } } func saveGameData() { let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray let documentsDirectory = paths.object(at: 0) as! NSString let path = documentsDirectory.appendingPathComponent("GameData.plist") print(path) let dict: NSMutableDictionary = ["itializerItem": "DoNotEverChangeMe"] //saving values dict.setObject(bedroomFloorID, forKey: BedroomFloorKey as NSCopying) dict.setObject(bedroomWallID, forKey: BedroomWallKey as NSCopying) //... //writing to GameData.plist dict.write(toFile: path, atomically: false) let resultDictionary = NSMutableDictionary(contentsOfFile: path) print("Saved GameData.plist file is --> \(String(describing: resultDictionary?.description))") }
}
相关文章推荐
- 提示用户输入信息,并将用户输入的写入一个文件,当用户输入的信息 是quit的时候,打印输出用户输入的总长度
- 考虑下列生成二进制的过程,编译器被用来生成单个单元的目标代码,链接器被用来将多个目标单元合并成一个程序二进制,链接器如何改变指令和数据到内存地址的绑定?需要什么信息从编译器传递给链接器,以协助完成链接
- 自己制作一个链表用来存储列表信息,并查找当前id信息,找上一条信息,下一条信息(信息浏览的时候方便使用)
- 关于plist文件存储方式(swift)
- 一个网站登录,然后下载网页源代码和文件的代码
- VS编译出现将元数据写入文件“x:/x/obj/Debug/xx.exe”时发生错误 --“存储空间不足,无法完成此操作。 ”
- 我这里有一段程序,用来在一个对话框里显示出一次http request的原始信息,不过使用Inet API做的
- 一个网站登录,然后下载网页源代码和文件的代码
- CxImage与OpenGL结合,用于读入多种格式的纹理以及用来把屏幕保存为各种格式的图像文件。 关于CxImage的文章,网上有许多,这里只介绍如何把CxImage与OpenGL结合起来,用于读
- 一个关于文件写入时乱码的故事
- 将元数据写入文件“.dll”时发生错误 --“存储空间不足,无法完成此操作。
- 关于 "CS0016: 未能写入输出文件" 的解决办法 编译器错误信息: CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\listenleaks\ae3daa38\e9fdb67
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)
- 将元数据写入文件“....dll”时发生错误 --“存储空间不足,无法完成此操作"
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)
- 关于 "CS0016: 未能写入输出文件" 的解决办法 编译器错误信息: CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/liste
- 一个存储用户信息的类,支持单一登录(经过网友提醒,标题已经修正)
- vs2005 将元数据写入文件 时发生错误 --“存储空间不足,无法完成此操作
- jsp中用cookie做的一个关于密码下次登录的时候能够自动显示小例子
- 关于一个模块中所有rgs文件中有注册项无法全部写入注册表的问题