關(guān)于ios存儲(chǔ)方式的匯總(原創(chuàng)):

1.獲取沙盒路徑:? NSString *home = NSHomeDirectory();

2.plist文件存儲(chǔ):

Plist注意:不能存儲(chǔ)自定義對(duì)象

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

簡(jiǎn)潔版plist存儲(chǔ):

- (IBAction)save:(id)sender {

// 獲取docpath

NSString *docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

// 拼接路徑

NSString *filepath = [docpath stringByAppendingPathComponent:@"x.plist"];

NSString *str = @"chuanzhi";

[str writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

- (IBAction)read:(id)sender {

// 獲取docpath

NSString *docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

// 拼接路徑

NSString *filepath = [docpath stringByAppendingPathComponent:@"x.plist"];

//? 獲取字符串

NSString *str = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@",str);

}

復(fù)雜版plist:

//存入

- (IBAction)save:(id)sender {??

? // 獲取路徑:NSSearchPath 尋找路徑耘沼,F(xiàn)orDirectories 要找的文件夾塌衰,InDomains 在哪個(gè)地方找? ?

?// NSDocumentDirectory Documents? ??

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];? ??

// 拼接路徑? ? NSString *filePath = [docPath stringByAppendingPathComponent:@"xxxx.plist"];? ?

?//? ? NSArray *arr = @[@"老馬",@"唐帥"];? ??

// 存儲(chǔ)? ??

//? ? [arr writeToFile:filePath atomically:YES];??

? // 創(chuàng)建字典? ? NSDictionary *dict = @{@"name":@"laoma",@"age":@"18"};? ??

//? ? 存儲(chǔ)字典? ? [dict writeToFile:filePath atomically:YES];}

// 讀取- (IBAction)read:(id)sender {//??

? // 獲取沙盒路徑//? ? NSString *home = NSHomeDirectory();//? ?

?// 拼接doc 路徑//? ? NSString *docPath = [home stringByAppendingString:@"/Documents"];//? ??

// 拼接plist路徑//? ? NSString *filePath = [docPath stringByAppendingString:@"/xx.plist"];//? ? // 讀取數(shù)組//? ? NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];??

? // 獲取doc路徑? ? NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];? ?

?// 拼接plist路徑? ? NSString *filePath = [docPath stringByAppendingPathComponent:@"xxxx.plist"];? ??

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];? ? NSLog(@"%@",dict[@"name"]);}

3.系統(tǒng)偏好設(shè)置存儲(chǔ):

// 好處:

1.不需要關(guān)心文件名

// 2.快速做鍵值對(duì)存儲(chǔ)

// 底層:就是封裝了一個(gè)字典

//存入

- (IBAction)save:(id)sender {

?// 取得ud

//? ? NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

? // 設(shè)置//? ? [ud setObject:@"laoma" forKey:@"name"];

//? ? [ud setInteger:18 forKey:@"age"];

//? ? [ud setBool:YES forKey:@"isOn"];

//? ? [ud synchronize];

//同步 強(qiáng)制寫(xiě)入? !!!一定要寫(xiě)}

//讀取

- (IBAction)read:(id)sender {

? // 獲取ud//? ? NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

}

4寸宵、歸檔解檔存儲(chǔ)

// 解析文件都會(huì)調(diào)用這個(gè)方法惧财,注意xib和story文件的加載用的就是這種方法進(jìn)行的初始化:

- (id)initWithCoder:(NSCoder *)aDecoder

{

// 只要父類遵守了NSCoding,就調(diào)用initWithCoder

// 先初始化父類

if (self = [super initWithCoder:aDecoder]) {

NSLog(@"%s",__func__);

}

return self;

}

而代碼初始化的底層調(diào)用:

// 通過(guò)代碼初始化的時(shí)候恤磷,調(diào)用init方法紊册,底層就會(huì)調(diào)用initWithFrame

- (instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame]) {

NSLog(@"%s",__func__);

}

return self;

}

@end

{#import

@interface Person : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic ,assign) NSInteger age;@end

#import "Person.h"@implementation Person// 告訴系統(tǒng)歸檔哪些屬性- (void)encodeWithCoder:(NSCoder *)aCoder {? ?

?[aCoder encodeObject:_name forKey:@"name"];? ?

?[aCoder encodeInteger:_age forKey:@"age"];}

// 告訴系統(tǒng)解檔哪些屬性,如何解檔

- (instancetype)initWithCoder:(NSCoder *)aDecoder?

{? ? if (self = [super init]) {? ? ? ?

?_name = [aDecoder decodeObjectForKey:@"name"];? ? ?

?? _age = [aDecoder decodeIntegerForKey:@"age"];? ?

?}? ??

return self;

}@end

#import@interface ViewController : UIViewController

@end

#import "ViewController.h"

#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController

// 歸檔

- (IBAction)save:(id)sender {

// 創(chuàng)建對(duì)象

Person *p = [[Person alloc] init];

p.name = @"zhangsan";

p.age = 18;

// docpath

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

//? 拼接路徑

NSString *fliePath = [docPath stringByAppendingPathComponent:@"person.data"];

// 歸檔

[NSKeyedArchiver archiveRootObject:p toFile:fliePath];

}

// 解檔

- (IBAction)read:(id)sender {

// docpath

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

//? 拼接路徑

NSString *fliePath = [docPath stringByAppendingPathComponent:@"person.data"];

// 解檔

Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:fliePath];

NSLog(@"%@",p.name);

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

}

5、鑰匙串存儲(chǔ):{

#import "ViewController.h"

#import "SSKeychain.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//要保存的數(shù)據(jù)

//? ? NSString *username = @"zhangsan";

//? ? NSString *password = @"lisi";

//存儲(chǔ)到鑰匙串

//pasetPassword 相當(dāng)于要存的值, account 相當(dāng)于key

//forService 標(biāo)識(shí)

//? ? [SSKeychain setPassword:username forService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

//從鑰匙串取數(shù)據(jù)

// NSString *username = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

// NSLog(@"%@",username);

NSString * username = @"zhangsan";

NSString * password = @"lisi";

[SSKeychain setPassword:username forService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

NSString * username = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

}

}

6燃逻、證書(shū)存儲(chǔ):

NSURL *url = [NSURL URLWithString:@"http://www.example.com"];

self.protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url.host

port:[url.port integerValue]

protocol:url.scheme

realm:nil

authenticationMethod:NSURLAuthenticationMethodHTTPDigest];

// 存

- (IBAction)save:(id)sender {

NSURLCredential *credential;

credential = [NSURLCredential credentialWithUser:@"houyuan" password:@"315213" persistence:NSURLCredentialPersistencePermanent];

[[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:self.protectionSpace];

}

// 取

- (IBAction)read:(id)sender {

NSURLCredential *credential;

NSDictionary *credentials;

credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:self.protectionSpace];

NSLog(@"User %@ already connected with password %@", credential.user, credential.password);

}

7闷袒、讀取硬件信息:

//? ViewController.m

#import "ViewController.h"

#import "SystemServices.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//獲取硬件信息

SystemServices *services = [SystemServices sharedServices];

//運(yùn)營(yíng)商的名字

NSLog(@"%@",services.allSystemInformation);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市岩梳,隨后出現(xiàn)的幾起案子囊骤,更是在濱河造成了極大的恐慌,老刑警劉巖冀值,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件也物,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡列疗,警方通過(guò)查閱死者的電腦和手機(jī)滑蚯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)抵栈,“玉大人告材,你說(shuō)我怎么就攤上這事」啪ⅲ” “怎么了斥赋?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)产艾。 經(jīng)常有香客問(wèn)我疤剑,道長(zhǎng),這世上最難降的妖魔是什么闷堡? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任隘膘,我火速辦了婚禮,結(jié)果婚禮上杠览,老公的妹妹穿的比我還像新娘弯菊。我一直安慰自己,他們只是感情好踱阿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布误续。 她就那樣靜靜地躺著,像睡著了一般扫茅。 火紅的嫁衣襯著肌膚如雪蹋嵌。 梳的紋絲不亂的頭發(fā)上碴萧,一...
    開(kāi)封第一講書(shū)人閱讀 51,370評(píng)論 1 302
  • 那天炫隶,我揣著相機(jī)與錄音,去河邊找鬼延窜。 笑死,一個(gè)胖子當(dāng)著我的面吹牛腺办,可吹牛的內(nèi)容都是我干的焰手。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼怀喉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼书妻!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起躬拢,我...
    開(kāi)封第一講書(shū)人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤躲履,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后聊闯,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體工猜,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年菱蔬,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了篷帅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拴泌,死狀恐怖魏身,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蚪腐,我是刑警寧澤叠骑,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站削茁,受9級(jí)特大地震影響宙枷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜茧跋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一慰丛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瘾杭,春花似錦诅病、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至讨阻,卻和暖如春芥永,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背钝吮。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工埋涧, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留板辽,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓棘催,卻偏偏與公主長(zhǎng)得像劲弦,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子醇坝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容