UI基礎整理 19

2021-07-09 14:11:52 字數 3856 閱讀 5463

//

//  viewcontroller.m

//  lesson19_初級資料持久化 //

//  created by floating_sh on 15/12/14.

//#import "viewcontroller.h"

#import "phone.h"

@inte***ce

viewcontroller()

@end

@implementation viewcontroller

- (void)viewdidload ;

// 準備檔案路徑

nsstring *path = nshomedirectory();

// 寫入檔案

[dict writetofile:path atomically:yes];

// 從檔案中獲取內容

nsdictionary *resultdict = [nsdictionary dictionarywithcontentsoffile:path];

nslog(@"%@",resultdict);

*/// 寫入檔案

// 獲取物件 /*

// 建立路徑

nsstring *path = nshomedirectory();

// 轉化為data物件

nsdata *data = uiimagejpegrepresentation(image, 1);//用與相對應的方法

// 寫入檔案

[data writetofile:path atomically:yes];

// 讀取內容

uiimageview *imgview = [[uiimageview alloc]initwithframe:self.view.bounds];

imgview.image = [uiimage imagewithcontentsoffile:path];

[self.view addsubview:imgview];

*/// 複雜物件寫入檔案/*

phone *phone = [phone new];

phone.color = @"greatgold";

phone.brand = @"huawei";

phone.price = @"1999";

// 建立data物件

nsmutabledata *mutabledata = [[nsmutabledata alloc]initwithcapacity:3];

// 建立歸檔物件

nskeyedarchiver *archiver = [[nskeyedarchiver alloc]initforwritingwithmutabledata:mutabledata];

// 歸檔

[archiver encodeobject:phone forkey:@"p8"];

// 完成歸檔

[archiver finishencoding];

//寫入檔案

nsstring *path = nshomedirectory();

[mutabledata writetofile:path atomically:yes];

// 反歸檔

// 獲取data物件

nsdata *data = [nsdata datawithcontentsoffile:path];

// 建立反歸檔物件

nskeyedunarchiver *unarchiver = [[nskeyedunarchiver alloc]initforreadingwithdata:data];

// 反歸檔

phone *resultphone = [unarchiver decodeobjectforkey:@"p8"];

// 完成反歸檔

[unarchiver finishdecoding];

nslog(@"%@ %@ %@",resultphone.color,resultphone.brand,resultphone.price);

// 歸檔不是資料持久化的方式,而是輔助完成將複雜物件轉化為nsdata物件的方式,真正完成資料持久化的方式仍然是write to file (寫入檔案)

//-------- 面試題 --------

// 資料持久化方式

// 1、屬性列表 (plist)

// 2、偏好設定 (nsuserdefault)

// 3、寫入檔案 (write to file)

// 4、資料庫 (sqlite)

// 5、core data

//oc裡面沒有真正意義上的深拷貝,除了歸檔和反歸檔

nslog(@"%@---%@",phone,resultphone);

*///檔案管理器

nsurlsessiondownloadtask

*downloadtask = [[

nsurlsession

sharedsession

]downloadtaskwithurl

:[nsurl

urlwithstring

completionhandler

:^(nsurl

*_nullable

location,

nsurlresponse

*_nullable

response,

nserror

* _nullable

error) ];

[downloadtask resume];

}- (void)didreceivememorywarning

@end

////  phone.h

//  lesson19_初級資料持久化 //

//  created by floating_sh on 15/12/14.

//#import

@inte***ce

phone :nsobject

@property(strong,nonatomic)

nsstring

*color;

@property(strong,nonatomic)

nsstring

*brand;

@property(strong,nonatomic)

nsstring

*price;

@end

////  phone.m

//  lesson19_初級資料持久化 //

//  created by floating_sh on 15/12/14.

//#import "phone.h"

@implementationphone

// 複雜物件想要實現資料持久化(寫入檔案),必須遵循nscoding協議,實現以下兩個方法

// 編碼

// 在這個方法中,我們需要實現每乙個需要歸檔的屬性,並標記key值

- (void)encodewithcoder:(nscoder *)acoder

// 解碼

// 在這個方法中,我們需要完成屬性的解碼操作,根據編碼時的key值一一對應獲取相應的值

UI基礎整理 10

uitableview編輯 1.讓tableview進入編輯狀態 void deletedataaction uibarbuttonitem barbutton 2.設定可編輯區域 bool tableview uitableview tableview caneditrowatindexpath ...

UI基礎整理 17

網路解析 get和post不同點 1.給伺服器傳輸資料的方式 get 通過 字串 post 通過data 2.傳輸資料的大小 get 字串最多255位元組 post 使用nsdata,容量超過1g 最大支援8eb 3.安全性 get 所有傳輸給伺服器的資料,顯示在 裡,類似於密碼的明文輸入,直接可見...

19 近體詩平仄整理 列舉

序 為了用python實現詩詞格律的分析,我研究了一段時間的詩律和詞律,現做整理分享給大家,以供需實現此類需求的朋友 快速 完整地理解格律。目錄講至此處,近體詩的簡單的平仄已經基本講完了。既然知道了格律,其實也沒有必要做下面的工作,只是因為出於數學的嚴謹性才出現了這一段內容。七言不過就是再五言的基礎...