swift3.0 属性文件plist的应用

来源:互联网 发布:家庭网络覆盖 编辑:程序博客网 时间:2024/05/16 01:59

       

         //获取工程中(私有的沙箱目录)" Data.plist "文件的路径

        private varDataPlistPath: String! = Bundle.main.path(forResource: "Data", ofType: "plist")


        //初始化localData.plist文件的路径;NSHomeDirectory:应用所存储的目录

        private var localPlistPath: String! = NSHomeDirectory() + "/Documents/localData.plist"


       //根据localPlistPath判断localData.plist文件是否存在

        let fileManager = FileManager.default
        let dbexits = fileManager.fileExists(atPath: self.localPlistPath)        
        if !dbexits {
            //如果文件不存在则创建localData.plist文件
            fileManager.createFile(atPath: self.localPlistPath, contents: nil, attributes: nil)

        }


      //将字典类型的数据写入localData.plist

        let rootDict = NSMutableDictionary()
        //设置boolean
        rootDict.setValue(true, forKey: "key_Boolean")
        //设置array
        rootDict.setValue(["one","two"], forKey: "key_Array")
        //设置numberInt
        rootDict.setValue(1, forKey: "key_Int")
        //设置numberFloat
        rootDict.setValue(1.2, forKey: "key_Float")
        //设置dictionary
        rootDict.setValue(["key1":"value1","key2":"value2"], forKey: "key_Dictionary")
        //设置date
        rootDict.setValue(NSDate(), forKey: "key_Date")

        //将rootDict写入localData.plist文件
        rootDict.write(toFile: self.localPlistPath, atomically: true)


       //读取localData.plist里的数据

       let rootDict = NSMutableDictionary(contentsOfFile: self.localPlistPath)


     //若要修改数据,就修改读取到的rootDict,再把rootDict重新写入localData.plist



原创粉丝点击