歸檔

1.將某個對象寫入文件時會調(diào)用*在這個方法中說清楚哪些屬性需要存儲

- (void)encodeWithCoder:(NSCoder*)encoder

{

[encoderencodeObject:self.noforKey:@"no"];

[encoderencodeInt:self.ageforKey:@"age"];

[encoderencodeDouble:self.heightforKey:@"height"];

}

2.*從文件中解析對象時會調(diào)用在這個方法中說清楚哪些屬性需要存儲

- (id)initWithCoder:(NSCoder*)decoder

{

if(self= [superinit]) {

//讀取文件的內(nèi)容

self.no= [decoderdecodeObjectForKey:@"no"];

self.age= [decoderdecodeIntForKey:@"age"];

self.height= [decoderdecodeDoubleForKey:@"height"];

}

returnself;

}

// 2.歸檔模型對象

// 2.1.獲得Documents的全路徑

NSString*doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];

// 2.2.獲得文件的全路徑

NSString*path = [docstringByAppendingPathComponent:@"stu.data"];

// 2.3.將對象歸檔

[NSKeyedArchiverarchiveRootObject:stutoFile:path];

NSString*doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];

// 2.獲得文件的全路徑

NSString*path = [docstringByAppendingPathComponent:@"stu.data"];

// 3.從文件中讀取MJStudent對象

MJStudent*stu = [NSKeyedUnarchiverunarchiveObjectWithFile:path];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容