OC~~~~~怎样使用enumerate开头的方法

1、关于NSString的方法

官方提供的方法:

/* In the enumerate methods, the blocks will be invoked inside an autorelease pool, so any values assigned inside the block should be retained.

*/

- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString * __nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

//    字符串

   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"girl" ofType:@"txt"];

   NSData *fileData = [NSData dataWithContentsOfFile:filePath];

   NSString *fileStr = [[NSString alloc]initWithData:fileData encoding:NSUTF8StringEncoding];

   //一行一行的读取

   [fileStr enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {

       NSLog(@"%@\n",line);

   }];

//    一个字符一字符的读取

   [fileStr enumerateSubstringsInRange:NSMakeRange(0, fileStr.length) options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {

       NSLog(@"tmp111===%@",substring);

   }];

2、关于NSArray的方法的使用

官方提供的方法:

- (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

NSArray *tmpAry = @[@"1",@"2",@"3"];

   /**

    NSEnumerationConcurrent 当前的排序状态

    NSEnumerationReverse    倒序排列

    */

   [tmpAry enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

       NSLog(@"tmpAry===%@\n",obj);

   }];

3、关于NSDictionary的方法的使用

官方方法:

- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

//    字典

   NSDictionary *tmpdic = @{@"1":@"q",@"2":@"w"};

   [tmpdic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

       NSLog(@"tmpdis:key=%@ value=%@\n",key,obj);

   }];

   /**

    NSEnumerationConcurrent 当前的排序状态

    NSEnumerationReverse    倒序排列

    */

   [tmpdic enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

       NSLog(@"tmpdis2:key=%@ value=%@\n",key,obj);

   }];

你可能感兴趣的:(OC~~~~~怎样使用enumerate开头的方法)