美文网首页
NSArray (YYAdd)学习

NSArray (YYAdd)学习

作者: 小希嘻 | 来源:发表于2018-07-06 11:31 被阅读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;
    }
    
    

    相关文章

      网友评论

          本文标题:NSArray (YYAdd)学习

          本文链接:https://www.haomeiwen.com/subject/ndpvuftx.html