NSArray (YYAdd)学习
2018-07-06 本文已影响12人
小希嘻
/*
typedef NS_ENUM(NSUInteger, NSPropertyListFormat) {
NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat, //明文
NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0, //XML
NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0 //二进制
};
typedef NS_OPTIONS(NSUInteger, NSPropertyListMutabilityOptions) {
NSPropertyListImmutable = kCFPropertyListImmutable, //整个数据容器不变
NSPropertyListMutableContainers = kCFPropertyListMutableContainers, //整个数据容器可变
NSPropertyListMutableContainersAndLeaves = kCFPropertyListMutableContainersAndLeaves //叶子节点和容器都可变
};
*/
+ (NSArray *)arrayWithPlistData:(NSData *)plist {
if (!plist) return nil;
NSArray *array = [NSPropertyListSerialization propertyListWithData:plist options:NSPropertyListImmutable format:NULL error:NULL];
if ([array isKindOfClass:[NSArray class]]) return array;
return nil;
}
+ (NSArray *)arrayWithPlistString:(NSString *)plist {
if (!plist) return nil;
NSData* data = [plist dataUsingEncoding:NSUTF8StringEncoding];
return [self arrayWithPlistData:data];
}
- (NSData *)plistData {
return [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListBinaryFormat_v1_0 options:kNilOptions error:NULL];
}
- (NSString *)plistString {
NSData *xmlData = [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListXMLFormat_v1_0 options:kNilOptions error:NULL];
if (xmlData) return xmlData.utf8String;
return nil;
}