/** NSUserDefaults */
// NSData届案、NSString、NSNumber罢艾、NSDate楣颠、NSArray、NSDictionar
static NSString* const key = @"key";
[[NSUserDefaults standardUserDefaults] setValue:@"key123" forKey:key];
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"key2"];
[[NSUserDefaults standardUserDefaults] setValue:@2 forKey:@"key3"];
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"key4"];
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"key5"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:key];
NSString *string2 = [[NSUserDefaults standardUserDefaults] valueForKey:@"key2"];
NSNumber *number3 = [[NSUserDefaults standardUserDefaults] valueForKey:@"key3"];
NSString *string4 = [[NSUserDefaults standardUserDefaults] objectForKey:@"key4"];
NSString *string5 = [[NSUserDefaults standardUserDefaults] objectForKey:@"key5"];
/** 屬性列表plist */
//序列化對象(serialized object):指可以被轉(zhuǎn)換為字節(jié)流以便于存儲(chǔ)到文件中或通過網(wǎng)絡(luò)進(jìn)行傳輸?shù)膶ο? /*
可以被序列化的類型只有如下幾種:
NSArray
NSMutableArray
NSDictionary
NSMutableDictionary
NSData
NSMutableData
NSString
NSMutableString
NSNumber
NSDate
*/
// 路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSString *path = [documentDirectory stringByAppendingPathComponent:@"data.plist"];
// 桌面路徑
NSString *desktopPath = @"/Users/xp/Desktop";
desktopPath = [desktopPath stringByAppendingPathComponent:@"data1.plist"];
// 存儲(chǔ)
NSArray *array = @[@1,@2,@3,@4];
[array writeToFile:desktopPath atomically:YES];
// 讀取
if ([[NSFileManager defaultManager]fileExistsAtPath:desktopPath]) {
// 確認(rèn)路徑存在
NSArray* arr = [[NSArray alloc]initWithContentsOfFile:desktopPath];
NSLog(@"arr: %@",arr);
}
/** NSKeyedArchiver 對象歸檔 */
// 在Cocoa中咐蚯,Archiver是另一種形式的序列化童漩,是任何對象都可實(shí)現(xiàn)的更常規(guī)的類型
// 只有遵守了NSCoding或 NSSecureCoding(更為安全的歸檔協(xié)議)協(xié)議,并且實(shí)現(xiàn)了協(xié)議里歸檔與解歸檔的方法的的類創(chuàng)建的對象才能夠進(jìn)行歸檔
// 最好也實(shí)現(xiàn)以下NSCopying,NSCopying與NSCoding一起實(shí)現(xiàn)好處在于允許復(fù)制對象春锋,使用數(shù)據(jù)模型對象時(shí)有較大的靈活性
// 桌面路徑
NSString *desktopPath = @"/Users/xp/Desktop";
desktopPath = [desktopPath stringByAppendingPathComponent:@"data1.plist"];
// 存儲(chǔ)
FourLines *lines = [[FourLines alloc] init];
lines.lines = @[@1,@3,@5,@10];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:lines forKey:@"kRootKey"];
[archiver finishEncoding];
[data writeToFile:desktopPath atomically:YES];
// 讀取
if ([[NSFileManager defaultManager]fileExistsAtPath:desktopPath]) {
NSData *data = [[NSMutableData alloc]initWithContentsOfFile:desktopPath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
FourLines *four = [unarchiver decodeObjectForKey:@"kRootKey"];
[unarchiver finishDecoding];
for (int i = 0; i < 4; i++) {
//to do
}
}
// 協(xié)議實(shí)現(xiàn)
#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.lines forKey:klinesKey];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
_lines = [aDecoder decodeObjectForKey:klinesKey];
}
return self;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
FourLines *copy = [[[self class] allocWithZone:zone] init];
NSMutableArray *linsCopy = [NSMutableArray array];
for (id line in self.lines) {
[linsCopy addObject:[line copyWithZone:zone]];
}
copy.lines = linsCopy;
return copy;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者