美文网首页
swift 文件读写

swift 文件读写

作者: JW_T | 来源:发表于2019-05-27 17:56 被阅读0次

        func jwSaveUser() {
           
            let dic:NSDictionary = self.mj_keyValues()
           
            let data:Data = NSKeyedArchiver.archivedData(withRootObject: dic)
           
            let doc:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
            let path = doc.appending("/loginUser.data")

            do{
               
                try data.write(to: URL(fileURLWithPath: path))
            }
            catch{
                print("r存入失败")
            }
           
        }
       
        func synchronousData(){
           
            let doc:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
            let path = doc.appending("/loginUser.data")
            let data:NSData? = NSData(contentsOfFile: path)
           
            if nil == data {}else{
                let dictionary = NSKeyedUnarchiver.unarchiveObject(with: data! as Data)! as! NSDictionary
               
                if dictionary.count > 0  {
                    UserModel.defaultManage.setValuesForKeys(dictionary as! [String : Any])
                    if UserModel.defaultManage.token.nullObject() == false {
                        UserModel.defaultManage.hasLogin = true
                    }
                }
               
            }
          
        }

    相关文章

      网友评论

          本文标题:swift 文件读写

          本文链接:https://www.haomeiwen.com/subject/qqhktctx.html