ios应用开发中plist的读写(Swift)
来源:互联网 发布:知乎精华化妆品 编辑:程序博客网 时间:2024/04/28 16:26
读取和存储数据是很多ios应用中常见的功能。有很多办法可以实现这个功能:NSUserDefaults、CoreData、使用plist等等。
今天要介绍的是如何使用plist来实现这个功能(使用swift语言)
下载资源
我们要使用一个GameData.plist文件,点击这里下载
打开它你将看到3个条目:
BedroomFloor(包括代码需要用到的具体floor的id)
BedroomWall(包括代码需要用到的具体wall的id)
XlnitializerItem(被代码使用 ,不要尝试去改变它)
创建你的项目
现在,让我们创建一个简单的项目。建立一个”Single View Application”
然后,将项目名设置为:ReadWritePlistTutorial并且选择Swift作为编程语言
设置你的项目
好,现在把GameData.plist文件拖进你的项目
要选择”Copy items if needed”并且目标地址选择”ReadWritePlistTutorial”
首先,我们要给plist Keys添加一些常量,添加在ViewController.swift文件中,在import UIKit之后
swift
// MARK: == GameData.plist Keys ==let BedroomFloorKey = "BrdroomFloor"let BedroomWallKey = "BedroomWall"// MARK: -
现在,我们要添加两个辅助变量,添加在viewDidLoad()方法前
swift
//MARK: == Variables ==var bedroomFloorID: AnyObject = 101var bedroomWallID: AnyObject = 101
现在ViewController.swift文件应该是这样:
添加逻辑
我们想在代码中使用plist的元素,所以我们要把它读到代码中转换为字典。拷贝这段代码到didReceiveMemoryWarning()方法下面
swift
func loadGameData() { // 获取GameData.plist的地址 let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray let documentsDirectory = paths[0] let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist") let fileManager = NSFileManager.defaultManager() //确认文件是否存在 if(!fileManager.fileExistsAtPath(path)) { // 如果不存在,就从bundle中拷贝一份默认文件 if let bundlePath = NSBundle.mainBundle().pathForResource("GameData", ofType: "plist") { let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath) print("Bundle GameData.plist file is --> \(resultDictionary?.description)") do{ try fileManager.copyItemAtPath(bundlePath, toPath: path) } catch{ 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.") // 使用这条命令删除目录中的文件 //fileManager.removeItemAtPath(path, error: nil) } let resultDictionary = NSMutableDictionary(contentsOfFile: path) print("Loaded GameData.plist file is --> \(resultDictionary?.description)") let myDict = NSDictionary(contentsOfFile: path) if let dict = myDict { //读取数值 bedroomFloorID = dict.objectForKey(BedroomFloorKey)! bedroomWallID = dict.objectForKey(BedroomWallKey)! //... } else { print("WARNING: Couldn't create dictionary from GameData.plist! Default values will be used!") }}
然后添加这段代码来存储对plist的改动
swift
func saveGameData() { let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray let documentsDirectory = paths.objectAtIndex(0) as! NSString let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist") let dict: NSMutableDictionary = ["XInitializerItem": "DoNotEverChangeMe"] //存储数值 dict.setObject(bedroomFloorID, forKey: BedroomFloorKey) dict.setObject(bedroomWallID, forKey: BedroomWallKey) //... //写入GameData.plist dict.writeToFile(path, atomically: false) let resultDictionary = NSMutableDictionary(contentsOfFile: path) print("Saved GameData.plist file is --> \(resultDictionary?.description)") }
通常我们需要在程序启动时读取plist的数据,所以把读取plist的方法添加到viewDidLoad()中
添加按钮
要存储数据让我们现在故事版中创建一个按钮
重命名为“Save”并且按住Ctrl并拖动它到ViewController.swift来创建一个连接
选择Action连接并且输入名字:SaveButtonTapped
现在我们要把bedroomFloorID变为999并且保存到plist。
添加下面的代码到刚刚创建的IBAction里面:
swift
//改变bedroomFloorID的值bedroomFloorID = 999//保存修改到plistsaveGameData()
编译并运行
点击“Save”按钮,你将看到log中打印的变化记录
我们完成了。如果你关闭模拟器,并再次编译运行,会看到BedroomFloorID还是999.
另外,你可以把Save按钮设置在屏幕的正中间
提示:
- ios应用开发中plist的读写(Swift)
- IOS学习 --- Swift 对于plist文件的读写
- iOS中plist的读写操作
- iOS开发(92)之 plist文件的读写
- iOS开发(92)之 plist文件的读写
- ios plist文件的读写
- iOS-plist文件的读写
- iOS plist文件的读写
- IOS Plist文件的读写
- iOS plist 文件的读写
- swift Plist文件的读写
- iOS 中plist中的对象的读写操作
- Swift plist文件读写
- ios开发入门- plist 文件读写
- iOS开发,plist文件读写那些事
- iOS开发,plist文件读写那些事
- iOS学习之 plist文件的读写
- IOS学习之 plist文件的读写
- iOS 将图片保存到相册
- UISlider
- 南大软院大神养成计划--php
- 南昌大学软件学院大神养成计划脚印-11.17
- Windows C编程中Win7隐藏任务栏图标方法
- ios应用开发中plist的读写(Swift)
- MFC 创建非模态对话框和销毁过程
- js-案例:省市连动下拉框
- Java反射探索-----从类加载说起
- 日记
- 南大软院大神养成计划--day02
- ELK学习8_ELK数据流传输过程_问题总结1
- poj 2291/3637 水题排序
- 解决百度地图com.baidu.platform.comjni.map.commonmemcache.JNICommonMemCache.Create:()J问题