NSArray+YYAdd的学习

2017-10-31  本文已影响38人  _阿南_
图片来之网络

数组获取

+ (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;
}

将NSData转化为NSArray。 NSArray没有提供将NSData转化为数组的方法,只是提供了+ (nullable NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;根据文件来获取数组。

NSPropertyListSerialization

提供了将NSData转化为id(包含NSArray和NSDictionary):

+ (nullable id)propertyListWithData:(NSData *)data options:(NSPropertyListReadOptions)opt format:(nullable NSPropertyListFormat *)format error:(out NSError **)error API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

也提供了id(包含NSArray和NSDictionary)转化为NSData:

+ (nullable NSData *)dataWithPropertyList:(id)plist format:(NSPropertyListFormat)format options:(NSPropertyListWriteOptions)opt error:(out NSError **)error API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

之前如果需要将NSDictionary或NSArray转化为NSData,那么会使用

随机获取数组中的元素

- (id)randomObject {
    if (self.count) {
        return self[arc4random_uniform((u_int32_t)self.count)];
    }
    return nil;
}

arc4random_uniform指定范围获取随机值。

// END iOS提供了很多种好的方法,但是好多没有直接使用,而是自己再实现一遍,那么!_!。

上一篇 下一篇

猜你喜欢

热点阅读