1 屬性列表(property list, plist)
屬性列表可以保存的類型有NSArray
,NSDictionary
,NSString
,NSNumber
,NSDate
,NSDate
以及它們對應的Mutable類。
NSDate
NSDate *date = [NSDate date];
NSLog(@"today is %@", date);
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24*3600)];
NSLog(@"yesterday is %@", yesterday);
NSDateFormatter
用來處理時間格式
NSData
const *string = "Hi there, this is a C string!";
NSData *data = [NSData dataWithBytes: string length: strlen(string)+1];
NSLog(@"data is %@", data);
// data is <48692074 68657265 2c207468 69732069 73206120 43207374 72696e67 2100>
NSLog(@"%lu byte string is '%s'", [data length], [data bytes]);
// 30 byte string is 'Hi there, this is a C string!'
- 寫入和讀取屬性列表
寫入
NSArray *phrase;
phrase = [NSArray arrayWithObjects:@"I", @"seem", @"to", @"be", @"a", @"verb", nil];
[phrase writeToFile:@"/tmp/verbiage.txt" atomically:YES];
verbiage.txt
內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>I</string>
<string>seem</string>
<string>to</string>
<string>be</string>
<string>a</string>
<string>verb</string>
</array>
</plist>
Mac電腦系統(tǒng)中
~/Library/Preferences
下的所有首選項文件和/System/Library/LaunchDaemons/
下的系統(tǒng)配置文件都是plist
讀取
NSArray *phrase2;
phrase2 = [NSArray arrayWithContentsOfFile:@"/tmp/verbiage.txt"];
NSLog(@"%@", phrase2);
-writeToFile:atomically:
方法中的atomically
表示是否首先將文件內(nèi)容保存在臨時文件中,再將臨時文件和原始文件交換奠旺。
- 修改對象類型
NSPropertyListSerialization
2 編碼對象
無法將對象信息表示為屬性列表類。
編碼和解碼(encoding and decoding) 和 序列化與反序列化(serialization and deserialization) 必須采用NSCoding
協(xié)議
在Interface Builder中的的對象就被序列化保存到磁盤中了亮航。
NSCoding
定義兩個方法咒唆,用于對象與NSDate
之間的轉換:
@protocol NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
@end
一個列子
@interface Thingie : NSObject <NSCoding>
{
NSString *name;
int magicNumber;
float shoeSize;
NSMutableArray *subThingies;
}
@property (copy) NSString *name;
@property int magicNumber;
@property float shoeSize;
@property (retain) NSMutableArray *subThingies;
- (id)initWithName: (NSString *) n
magicNumber: (int) mn
shoeSize: (float) ss;
@end // Thingie
Thingie
采用了NSCoding
協(xié)議,要實現(xiàn)編碼和解碼響應的方法聪全。
@implementation Thingie
@synthesize name;
@synthesize magicNumber;
@synthesize shoeSize;
@synthesize subThingies;
- (id)initWithName: (NSString *) n
magicNumber: (int) mn
shoeSize: (float) ss
{
if (self = [super init])
{
self.name = n;
self.magicNumber = mn;
self.shoeSize = ss;
self.subThingies = [NSMutableArray array];
}
return (self);
}
- (void) encodeWithCoder: (NSCoder *) coder
{
[coder encodeObject: name
forKey: @"name"];
[coder encodeInt: magicNumber
forKey: @"magicNumber"];
[coder encodeFloat: shoeSize
forKey: @"shoeSize"];
[coder encodeObject: subThingies
forKey: @"subThingies"];
} // encodeWithCoder
- (id) initWithCoder: (NSCoder *) decoder
{
if (self = [super init])
{
self.name = [decoder decodeObjectForKey: @"name"];
self.magicNumber = [decoder decodeIntForKey: @"magicNumber"];
self.shoeSize = [decoder decodeFloatForKey: @"shoeSize"];
self.subThingies = [decoder decodeObjectForKey: @"subThingies"];
}
return (self);
} // initWithCoder
- (NSString *) description
{
NSString *description =
[NSString stringWithFormat: @"%@: %d/%.1f %@",
name, magicNumber, shoeSize, subThingies];
return (description);
} // description
@end // Thingie