1.归档:把对象转成 NSData 唯一方式
NSCoding 协议
NSData*data = [NSKeyedArchiverarchivedDataWithRootObject:self.Myview];
[datawriteToFile:@"/Users/tao-pc/Desktop/name.arch"atomically:YES];
反归档:把 data 转回对象
NSData*data = [NSDatadataWithContentsOfFile:@"/Users/tao-pc/Desktop/name.arch"];
UIView*view = [NSKeyedUnarchiverunarchiveObjectWithData:data];
自定义归档
@interfacePerson :NSObject
- (void)encodeWithCoder:(NSCoder*)aCoder{
[aCoderencodeObject:self.nameforKey:@"name"];
[aCoderencodeInt:self.ageforKey:@"age”];
}
- (nullableinstancetype)initWithCoder:(NSCoder*)aDecoder{
self= [superinit];
if(self) {
self.name= [aDecoderdecodeObjectForKey:@"name"];
self.age= [aDecoderdecodeIntForKey:@"age"];
}
returnself;
}
//归档
// NSData *data = [NSKeyedArchiver archivedDataWithRootObject:p];// [data writeToFile:@"/Users/tarena/Desktop/person.arch" atomically:YES];
//自定义对象反归档
NSData*data = [NSDatadataWithContentsOfFile:@"/Users/tarena/Desktop/person.arch"];
Person*p = [NSKeyedUnarchiverunarchiveObjectWithData:data];
XML_Dom
+(NSMutableArray*)parseSonsWithData:(NSData*)data{
NSMutableArray*songs = [NSMutableArrayarray];
//创建解析对象
TBXML*tbxml = [[TBXMLalloc]initWithXMLData:dataerror:nil];
//得到根元素
TBXMLElement*songsEle = tbxml.rootXMLElement;
//通过父元素获取某个名字的子元素
TBXMLElement*songEle = [TBXMLchildElementNamed:@"song"parentElement:songsEle];
//遍历每一首歌曲
while(songEle) {
//获取歌曲的各个数据
TBXMLElement*titleEle = [TBXMLchildElementNamed:@"title"parentElement:songEle];
TBXMLElement*artistEle = [TBXMLchildElementNamed:@"artist"parentElement:songEle];
TBXMLElement*timeEle = [TBXMLchildElementNamed:@"time"parentElement:songEle];
NSString*title = [TBXMLtextForElement:titleEle];
NSString*artist = [TBXMLtextForElement:artistEle];
NSString*time = [TBXMLtextForElement:timeEle];
//获取属性的专辑名称
NSString*albumName = [TBXMLvalueOfAttributeNamed:@"albumName"forElement:songEle];
Song*song = [Songnew];
song.title= title;
song.artist= artist;
song.time= time;
song.albumName= albumName;
[songsaddObject:song];
//获取剩下的其余兄弟元素
songEle = [TBXMLnextSiblingNamed:@"song"searchFromElement:songEle];
}
returnsongs;
}
CoreData: 核心数据
OS 提供的封装了 sqlite 数据库的 api 框架
每个 ViewController 的 NavigationController 都时不同的,他的的 Controllers 个数不同
基本的使用
1.导入AppDelegate 获取到他的AppDelegate.managedObjectContext
#import"AppDelegate.h"
AppDelegate*app = [UIApplicationsharedApplication].delegate;
2.插入数据
Student*stu = [NSEntityDescriptioninsertNewObjectForEntityForName:@"Student"inManagedObjectContext:app.managedObjectContext];
[appsaveContext];
3.查询数据
NSFetchRequest*request = [[NSFetchRequestalloc]initWithEntityName:@"Student"];
NSArray*sutdents = [app.managedObjectContextexecuteFetchRequest:requesterror:nil];
4.获取之后修改,再跟新
Player*player =self.players[indexPath.row];
5.删除数据 获取到删除
Player*player =self.players[indexPath.row];
AppDelegate*app = [UIApplicationsharedApplication].delegate;
[app.managedObjectContextdeleteObject:player];
[appsaveContext];