swift 文件读写

    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 文件读写)