关于swift下归档解档遇到的坑

单个对象归档,对使用nscoding,nskeyed..理解更深刻了

列子

  • 在使用对对象数组归档解档 要特别小心 iOS 下对于自定义的对象
    要实现归档操作必须注意
    只有使用nsdata作为中间者转换具体思路
    归档 customclass ->实现nscoding->NSKeyedArchiver.archivedDataWithRootObject一个实例到nsdata->归档这个nsdata
    解档 过程相反NSKeyedUnarchiver.unarchive as nsdata->cutsom=NSKeyedUnarchiver.narchiveObjectWithData->终于拿到

WLPlayer

   struct WlPlayerkey 
{        
  static  let  playernamekey="name"    
  static  let playerimagekey="displayimg"        
  static  let  playerpath="/players.data" 
  }       
var   name:String!    
var    displayimg:String!      //得到沙盒路径存到缓存中   static func  getfilepath()->String 
  {             
 let  cachespath:String=NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask, true)[0]   
    let  path:String= cachespath.stringByAppendingString(WlPlayerkey.playerpath)                
 return  path    
    }    //保存归档   
static func  saveplayertofile(newplayer:WLPlayer?)     ->BooleanType    
{   
    if (newplayer == nil)      
 {            return  false    
   }                  
return   NSKeyedArchiver.archiveRootObject(newplayer!, toFile:WLPlayer.getfilepath())           }    //得到all玩家信息   
  static  func   getallplayers()    ->WLPlayer
 {           
    let data=NSKeyedUnarchiver.unarchiveObjectWithFile(WLPlayer.getfilepath()) as! WLPlayer   
    print( data.info())   
    return   data   
    }   
func  info()->String    
{        return "玩家"+self.name
   } 
    required convenience init?(coder aDecoder: NSCoder)
{           let  aname=aDecoder.decodeObjectForKey(WlPlayerkey.playernamekey)     let imagepath=aDecoder.decodeObjectForKey(WlPlayerkey.playerimagekey)          
  self.init(name:aname as! String,displayimg:imagepath as! String)      
 }  
 func  encodeWithCoder(aCoder: NSCoder) {                aCoder.encodeObject(self.name,forKey: WlPlayerkey.playernamekey)          aCoder.encodeObject(self.displayimg,forKey: WlPlayerkey.playerimagekey)        
   }
  init?(name:String,displayimg:String) 
{        
  super.init()   
    if  name.characters.count>0    
   {           
  self.name=name      
 self.displayimg=displayimg      
    }       
else      
 {                               
   return  nil       
}    
}    //判断是否为一个玩家  
 func  judgesameplayer(otherplayer:WLPlayer?)->Bool    {        
    if    (otherplayer != nil)    
   {        
   if  otherplayer!.name==self.name       
    {          
    return  true  
    }            
return  false    
   }       
return  false           
}   
}//多个对象的存储使用解档归档  ,    轻量级 存储数据(的对象必须实现nscoding 两个方法,实际上才能  把 你的对象转成nsdata 类型的)

使用

  let  newp1=Person.init(name: "王nima", age: 13)       
 let  newp2=Person.init(name: "王s", age: 13)      
  let  newp3=Person.init(name: "雨豪", age: 13)       
 let  newp4=Person.init(name: "王雨", age: 14)        
let  newp5=Person.init(name: "王", age: 16)      
  let  newp6=Person.init(name: "妄图图", age: 13)      
  let  allpersons=[newp1,newp2,newp3,newp4,newp5,newp6]                allperson.addObject(newp1!)          allperson.addObject(newp2!)          allperson.addObject(newp3!)          allperson.addObject(newp4!)        allperson.addObject(newp5!)        allperson.addObject(newp6!)        print(getfinalpath())//      
 let   data=NSKeyedArchiver.archivedDataWithRootObject(newp6!)//       
 //        //       NSUserDefaults.standardUserDefaults().setObject(data, forKey: "new6")//        //    
   let getdata=NSUserDefaults.standardUserDefaults().objectForKey("new6") as! NSData//        //        //    
    let getnewperson=NSKeyedUnarchiver.unarchiveObjectWithData(getdata)//         as!  Person//        //        print(getnewperson.name)            
 let   datas=NSKeyedArchiver.archivedDataWithRootObject(allperson)                                NSKeyedArchiver.archiveRootObject(datas, toFile: getfinalpath())                                      
  let   getdatas=NSKeyedUnarchiver.unarchiveObjectWithFile(getfinalpath()) as! NSData             
   let   somearry=NSKeyedUnarchiver.unarchiveObjectWithData(getdatas) as!  NSMutableArray          
  var   getpersons:[Person]?=[Person]()            
    for   ids in  somearry     
   {           
 if   ids  is  Person       
     {             
  let   a = ids as! Person           
    getpersons?.append(a)        
    print(a.name)                 
    }       
 }
  • swift归档中可能会遇到的小坑
    针对enum类型的归档,需要在归档的时候需要使用rawvalue(type),不能直接使用enum类型直接归档,解档需要将其取出构造enum类型的,就是注意类型转换的问题.当然你也可以使用enum 类型不支持nscoding协议必须是nsobject对象的才能使用.不过在新的swift4中是可以支持codable协议

你可能感兴趣的:(关于swift下归档解档遇到的坑)