plist(Property List)是属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息。
plist文件支持的数据类型总共有7种,分别是Array、Dictionary、Boolean、Data、Date、Number、String。(其中Date的存储有点怪异)
1、首先创建一个plist属性列表文件,用于读取
2、右击plist文件,有2种打开方式
3、使用 "Property List"方式打开,在空白处右击,会出现如下菜单。
选择“Add Row”,可以为所选项添加一个子项目;选择“Value Type”可以修改选中项的类型。
5、左为“Property List”打开方式,右为“Source Code”打开方式
从2张的对比图可以看到Date类型的存储的怪异之处,test1 -> "May 31" 结果在xml中显示的确是05-30。
这 "T"应该是表示后面接的是时间的意思
然而9:22:33PM在xml中存储的是13:22:33Z,这是什么鬼。
希望有人能不吝赐教,告诉我这些怎么理解,谢谢。
//
// ViewController.m
// TableViewDemo
//
// Created by 555chy on 6/13/16.
// Copyright © 2016 555chy. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//读取plist
NSString *myPropertyListPath = [[NSBundle mainBundle] pathForResource:@"MyPropertyList" ofType:@"plist"];
NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:myPropertyListPath];
//plist path /Users/555chy/Library/Developer/CoreSimulator/Devices/D0FBF89D-87D2-4A88-BFB3-581EBCD3DDA5/data/Containers/Bundle/Application/BA6ABF0A-B721-42D3-ABFA-1247A87DD1DA/
//说明了bundle中的文件并不是以我们为他命名的哪个文件名存在的,而是以一个UUID(8-4-4-4-12)来命名的
NSLog(@"plist path %@", myPropertyListPath);
//如果是空的会输出(null)
NSLog(@"%@", plistData);
//存入plist
/*
typedef NS_ENUM(NSUInteger, NSSearchPathDirectory) {
NSApplicationDirectory = 1, // supported applications (Applications)
NSDemoApplicationDirectory, // unsupported applications, demonstration versions (Demos)
NSDeveloperApplicationDirectory, // developer applications (Developer/Applications). DEPRECATED - there is no one single Developer directory.
NSAdminApplicationDirectory, // system and network administration applications (Administration)
NSLibraryDirectory, // various documentation, support, and configuration files, resources (Library)
NSDeveloperDirectory, // developer resources (Developer) DEPRECATED - there is no one single Developer directory.
NSUserDirectory, // user home directories (Users)
NSDocumentationDirectory, // documentation (Documentation)
NSDocumentDirectory, // documents (Documents)
NSCoreServiceDirectory, // location of CoreServices directory (System/Library/CoreServices)
NSAutosavedInformationDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 11, // location of autosaved documents (Documents/Autosaved)
NSDesktopDirectory = 12, // location of user's desktop
NSCachesDirectory = 13, // location of discardable cache files (Library/Caches)
NSApplicationSupportDirectory = 14, // location of application support files (plug-ins, etc) (Library/Application Support)
NSDownloadsDirectory NS_ENUM_AVAILABLE(10_5, 2_0) = 15, // location of the user's "Downloads" directory
NSInputMethodsDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 16, // input methods (Library/Input Methods)
NSMoviesDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 17, // location of user's Movies directory (~/Movies)
NSMusicDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 18, // location of user's Music directory (~/Music)
NSPicturesDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 19, // location of user's Pictures directory (~/Pictures)
NSPrinterDescriptionDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 20, // location of system's PPDs directory (Library/Printers/PPDs)
NSSharedPublicDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 21, // location of user's Public sharing directory (~/Public)
NSPreferencePanesDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 22, // location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes)
NSApplicationScriptsDirectory NS_ENUM_AVAILABLE(10_8, NA) = 23, // location of the user scripts folder for the calling application (~/Library/Application Scripts/code-signing-id)
NSItemReplacementDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 99, // For use with NSFileManager's URLForDirectory:inDomain:appropriateForURL:create:error:
NSAllApplicationsDirectory = 100, // all directories where applications can occur
NSAllLibrariesDirectory = 101, // all directories where resources can occur
NSTrashDirectory NS_ENUM_AVAILABLE(10_8, NA) = 102 // location of Trash directory
};
*/
/*
typedef NS_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
NSUserDomainMask = 1, // user's home directory --- place to install user's personal items (~)
NSLocalDomainMask = 2, // local to the current machine --- place to install items available to everyone on this machine (/Library)
NSNetworkDomainMask = 4, // publically available location in the local area network --- place to install items available on the network (/Network)
NSSystemDomainMask = 8, // provided by Apple, unmodifiable (/System)
NSAllDomainsMask = 0x0ffff // all domains: all of the above and future items
};
*/
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSLog(@"document path count = %ld \n %@", [documentPaths count], documentPaths);
NSString *testPlistPath = [[documentPaths objectAtIndex:0] stringByAppendingPathComponent:@"test.plist"];
NSLog(@"after add, plist path = %@", testPlistPath);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
//dictionary只能存入id类型的变量,要将bool等基本数值类型的变量转换为NSNumber后再存
NSNumber *boolNum = [NSNumber numberWithBool:YES];
[dictionary setObject:boolNum forKey:@"isMan"];
//获取当前时间
NSDate *currentDate = [NSDate date];
[dictionary setObject:currentDate forKey:@"date"];
NSLog(@"currentDate = %@", currentDate);
//时间戳
NSTimeInterval timeInterval = [currentDate timeIntervalSince1970];
NSNumber *doubleNum = [NSNumber numberWithDouble:timeInterval];
[dictionary setObject:doubleNum forKey:@"timeInterval"];
NSLog(@"timeInterval = %lf", timeInterval);
//将时间转换为指定的格式
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss SS a"];
NSString *dateStr = [dateFormatter stringFromDate:currentDate];
[dictionary setObject:dateStr forKey:@"dateStr"];
NSLog(@"dateStr = %@", dateStr);
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SS a"];
NSLog(@"dateStr = %@", [dateFormatter stringFromDate:currentDate]);
//由于日期的存储比较奇怪,现在来测试一下: 发现day设置1是上个月的最后一天,2才是这个月的第一天
for(int i=1;i<=12;i++) {
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:i];
[dateComponents setDay:1];
[dateComponents setYear:2016];
[dateComponents setHour:01];
[dateComponents setMinute:02];
[dateComponents setSecond:03];
[dateComponents setNanosecond:04];
//NSGregorianCalendar 公历 is deprecated
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *testDate = [calendar dateFromComponents:dateComponents];
[dictionary setObject:testDate forKey:[NSString stringWithFormat:@"date %2d %@", i-1, @"end"]];
NSLog(@"date %2d %s = %@", i-1, "end", testDate);
[dateComponents setDay:2];
testDate = [calendar dateFromComponents:dateComponents];
[dictionary setObject:testDate forKey:[NSString stringWithFormat:@"date %2d %s", i, "begin"]];
NSLog(@"date %2d %s = %@", i, "begin", testDate);
}
//将字符串转为16进制
NSString *str = @"hello world";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//可以输出data的16进制数组<xxxxxxxxx xxxxxxxx ........>
NSLog(@"raw data = %@", data);
//转成bytes以后会变成一个数组,如果用%s会因为没有‘\0’结束符而让输出显得有点怪异
NSLog(@"data bytes = %s", data.bytes);
NSLog(@"newStr = %@", newStr);
NSString *str2 = @"hello world hello world";
NSData *data2 = [str2 dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"raw data2 = %@", data2);
NSLog(@"data2 bytes = %s", data2.bytes);
[dictionary setObject:data forKey:@"data"];
BOOL result = [dictionary writeToFile:testPlistPath atomically:YES];
NSLog(@"write %@", result?@"succ":@"fail");
NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:testPlistPath];
NSLog(@"new dictionary = %@", newDictionary);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行截图如下
从运行截图中,可以看到日期的存储甚是奇怪。上文已经提到一部分了,这里就不在赘述了。
我发现一点很有意思,那就是设置dateComponent的day(日期)为1,显示的是上一个月的最后1天,而day为2时才是这个月的第1天