你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS之[屬性列表]

IOS之[屬性列表]

編輯:關於IOS

@implementation JamesWongViewController

 

- (void)viewDidLoad

{

[super viewDidLoad];

 

[self writePerson];

}

 

#pragma mark 嘗試寫Person

// 不能通過writeToFile將一個普通對象寫入文件中

// writeToFile會刪掉以前存在的文字,創建一個新的文件

- (void)writePerson {

Person *person = [[[Person alloc] init] autorelease];

person.name = @"JamesWong";

person.age = 10;

 

//NSArray *array = [NSArray arrayWithObject:person];

 

// 注意:第一個參數是NSDocumentDirectory,說明要搜索Documents目錄

// NSUserDomainMask:在應用沙盒中搜索

// 如果第3個參數寫NO:~/Documents

// 在iOS平台,這個函數返回的數組中只有1個結果

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

 

NSString *path = [documents stringByAppendingPathComponent:@"array.plist"];

 

NSArray *array = [NSArray arrayWithObjects:@"1", nil];

[array writeToFile:path atomically:YES];

}

 

#pragma mark 將字典寫入屬性列表文件中

- (void)writeDict {

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setObject:@"JamesWong" forKey:@"name"];

[dict setObject:[NSNumber numberWithInt:10] forKey:@"age"];

 

// 獲取應用沙盒的根路徑

NSString *home = NSHomeDirectory();

NSString *documents = [home stringByAppendingPathComponent:@"Documents"];

// 屬性列表的默認拓展名是plist

NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];

 

[dict writeToFile:path atomically:YES];

}

 

#pragma mark 從屬性列表文件中讀取字典

- (void)readDict {

// 獲取應用沙盒的根路徑

NSString *home = NSHomeDirectory();

NSString *documents = [home stringByAppendingPathComponent:@"Documents"];

// 屬性列表的默認拓展名是plist

NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];

 

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

 

NSLog(@"%@", dict);

}

@end

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved