NSKeyedArchiever的一些屬性(歸檔)

2021-06-29 08:52:27 字數 2828 閱讀 9026

nskeyedarchiver

//路徑的巨集

#define path @

"/users/mac/desktop/data.archive" //

自定義類歸檔後的檔案的路徑

#define custom_path @

"/users/mac/desktop/dog.archive"

//歸檔:物件序列化,把物件按照一定的資料格式儲存到檔案裡面,方便從檔案中讀取回物件。 //

解檔:反歸檔,讀檔。

//1.

系統類的歸檔 //

第乙個引數:要歸檔的物件 //

第二個引數:路徑

歸檔 nsstring *str = @"dataarchive";

bool ret = [nskeyedarchiver

archiverootobject:str tofile:path];

if (ret) {

nslog(@"

歸檔成功

");else{

nslog(@"

歸檔失敗

");解檔:讀檔,反歸檔 //

引數:路徑

nsstring *str0 = [nskeyedunarchiver

unarchiveobjectwithfile:path];

nslog(@"str0 = %@",str0);

//如果要在原有的資料基礎上新增新資料(歸檔

/plist

),需要先獲取到原有的資料,然後把原有資料和這次要新增的資料放到乙個陣列或字典中,然後把陣列或字典重新進行歸檔(寫入

plist檔案)

。 nsarray *array0 = @[

@"one",@"two",@"three"

];//str0 array0 --> nsarray nsdictionary

nsarray *finalarr = @[str0,array0];

[nskeyedarchiver

archiverootobject:finalarr tofile:path];

id obj = [nskeyedunarchiver

unarchiveobjectwithfile:path];

nslog(@"obj = %@",obj);

//2.

自定義類的歸檔

:自定義的類要想進行永久性的儲存,只能通過歸檔的方式。

dog *dog = [[dog

alloc]init];

dog.name = @"xiaobai";

dog.age = 4;

bool customret = [nskeyedarchiver

archiverootobject:dog tofile:custom_path];

if (customret) {

nslog(@"

自定義類歸檔成功

");else{

nslog(@"

自定義類歸檔失敗

");id obj1 = [nskeyedunarchiver

unarchiveobjectwithfile:custom_path];

nslog(@"

自定義類解歸檔結果

:%@",obj1);

dog.h

//遵守協議:使用

<>

表示協議

,<>

裡面寫協議的名字

@inte***ce dog : nsobject

//自定義的類要想進行歸檔,這個自定義的類得遵守

nscoding

協議,並且實現協議中的方法

@property(copy ,nonatomic) nsstring *name; //名字

@property(assign ,nonatomic)  int age;     //年齡

dog.m

@implementation dog

//實現協議中的方法 //

歸檔要用到的方法:對要歸檔的內容進行歸檔操作

-(void)encodewithcoder:(nscoder *)acoder{

//物件的歸檔使用

encodeobject:方法

[acoder encodeobject:self.name

forkey:@"name"];

//基礎資料型別的歸檔使用相應的

encodeint:方法

[acoder encodeint:self.age

forkey:@"age"];

//解檔(讀檔,反歸檔)要用到的方法

-(id)initwithcoder:(nscoder *)adecoder{

//如果當前類的父類也是遵守

nscoding

協議的類

--> self = [super initwithcoder:]

if (self = [super

init]) {

//先判斷父類是否初始化成功,如果父類初始化成功,再進行自己的初始化操作

self.name = [adecoder decodeobjectforkey:@"name"];

self.age = [adecoder decodeintforkey:@"age"];

return

self;

-(nsstring *)description{

nsstring *str = [nsstring

stringwithformat:@"

姓名= %@,

年齡= %d",self.name,self.age];

return str;

layout的一些屬性

1.android visibility gone 其有三個屬性 visible顯示 invisible顯示黑背景條 gone不顯示 2.android scaletype屬性 imageview.scaletype.center android scaletype center 以原圖的幾何中心點...

UIImageView的一些屬性

1.基本概念 self setexclusivetouch yes 只能點乙個 設定window的背景 用進行配色 要保證的大小和 檢視的frame一樣大 使用者互動 預設是no。uilabel和uiimageview是需要手動開啟互動的。iv.userinteractionenabled yes ...

NSURLConnection的一些屬性(狀態碼)

當客戶端接收到服務端的響應時,會呼叫這個方法 void connection nsurlconnection connection didreceiveresponse nsurlresponse response 狀態碼 的屬性 繼承自nsurlresponse 狀態碼 200 請求成功 404 ...