1.歸檔:把對(duì)象轉(zhuǎn)成 NSData 唯一方式
NSCoding 協(xié)議
NSData*data = [NSKeyedArchiverarchivedDataWithRootObject:self.Myview];
[datawriteToFile:@"/Users/tao-pc/Desktop/name.arch"atomically:YES];
反歸檔:把 data 轉(zhuǎn)回對(duì)象
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];
//自定義對(duì)象反歸檔
NSData*data = [NSDatadataWithContentsOfFile:@"/Users/tarena/Desktop/person.arch"];
Person*p = [NSKeyedUnarchiverunarchiveObjectWithData:data];
XML_Dom
+(NSMutableArray*)parseSonsWithData:(NSData*)data{
NSMutableArray*songs = [NSMutableArrayarray];
//創(chuàng)建解析對(duì)象
TBXML*tbxml = [[TBXMLalloc]initWithXMLData:dataerror:nil];
//得到根元素
TBXMLElement*songsEle = tbxml.rootXMLElement;
//通過父元素獲取某個(gè)名字的子元素
TBXMLElement*songEle = [TBXMLchildElementNamed:@"song"parentElement:songsEle];
//遍歷每一首歌曲
while(songEle) {
//獲取歌曲的各個(gè)數(shù)據(jù)
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: 核心數(shù)據(jù)
OS 提供的封裝了 sqlite 數(shù)據(jù)庫(kù)的 api 框架
每個(gè) ViewController 的 NavigationController 都時(shí)不同的藐唠,他的的 Controllers 個(gè)數(shù)不同
基本的使用
1.導(dǎo)入AppDelegate 獲取到他的AppDelegate.managedObjectContext
#import"AppDelegate.h"
AppDelegate*app = [UIApplicationsharedApplication].delegate;
2.插入數(shù)據(jù)
Student*stu = [NSEntityDescriptioninsertNewObjectForEntityForName:@"Student"inManagedObjectContext:app.managedObjectContext];
[appsaveContext];
3.查詢數(shù)據(jù)
NSFetchRequest*request = [[NSFetchRequestalloc]initWithEntityName:@"Student"];
NSArray*sutdents = [app.managedObjectContextexecuteFetchRequest:requesterror:nil];
4.獲取之后修改,再跟新
Player*player =self.players[indexPath.row];
5.刪除數(shù)據(jù) 獲取到刪除
Player*player =self.players[indexPath.row];
AppDelegate*app = [UIApplicationsharedApplication].delegate;
[app.managedObjectContextdeleteObject:player];
[appsaveContext];