您的位置:首页 > 移动开发 > Swift

关于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 {

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))")

}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swift
相关文章推荐