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

2016-12-14  本文已影响1053人  lotawei

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

列子

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)                 
    }       
 }
上一篇下一篇

猜你喜欢

热点阅读