iOS:NS中 索引Index、位置Range、集合set等辅助类

本文介绍了iOS开发中常见的三种数据结构:NSIndexPath用于表示树状结构中的节点路径,NSIndexSet用于存储唯一整数索引,NSRange用于描述一系列元素的范围。它们分别在UITableView、数据过滤和字符串处理等方面有广泛应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

========================NSIndexPath========================

--简介:The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path。NSIndexPath类代表着“嵌套数组的树”中得某节点的路径。如下图:

         NSIndexPath 让你精确指定一个树结构 data structure 里面的某个节点的数据。比如你有一个 NSArray, 里面很多节点,每个节点又是一个 NSArray,每个节点的这个里面又是一个NSArray,然后下面又是一个 NSArray,这样简单说起来,你有一个四层的 NSarray ,每层下面都有好多个 NSArray。然后你造一个NSIndexPath 1.3.4.2。你就可以拿它访问,第一层的第三个节点之下的第四个节点的第二个节点的数据

------------常用方法和属性------------

--用Index 或者 index数组和路径的长度 创建NSIndexPath;

    + (instancetype)indexPathWithIndex:(NSUInteger)index;
    + (instancetype)indexPathWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length;
    - (instancetype)init;	/* designated initializer */
    - (instancetype)initWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length;	/* designated initializer */
 --修改:注意返回的是 一个新的对象,不是原来的对象基础上改得。
    - (NSIndexPath *)indexPathByAddingIndex:(NSUInteger)index;
    - (NSIndexPath *)indexPathByRemovingLastIndex;
--访问属性、比较
    - (NSUInteger)indexAtPosition:(NSUInteger)position;
    - (NSUInteger)length;  //路径包含节点个数 或 “路径长度"
    // comparison support
    - (NSComparisonResult)compare:(NSIndexPath *)otherObject;
--例:
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:1];
NSLog(@"%@  length:%D ",indexPath,indexPath.length);  //<NSIndexPath: 0x8a0ea20> {length = 1, path = 1}  length:1 ​
indexPath = [indexPath indexPathByAddingIndex:24];    
NSLog(@"%@  length:%D ",indexPath,indexPath.length);  //<NSIndexPath: 0x8c54090> {length = 2, path = 1 - 24}  length:2 
NSLog(@"postion:%d",[indexPath indexAtPosition:1]);  //postion:24

备注:

1.在UITableview.h中定义着NSIndexPath的分类。用它来表示tableview中某section中某行,为了方便加了一些属性和方法:

    // This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
    @interface NSIndexPath (UITableView)    
    + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
    @property(nonatomic,readonly) NSInteger section;
    @property(nonatomic,readonly) NSInteger row;

======================== NSIndexSet========================

--简介:The NSIndexSet class represents an immutable collection of unique unsigned integers, known as indexes because of the way they are used。You use index sets in your code to store indexes into some other data structure.比如NSIndexSet 用来让你从某个 data structure 里面提取一部分东西出来成为一个新的东西。比如你有一个 NSArray, 里面是(one, two, three, four, five),然后你造了个 indexSet 的内容是 1,2,3,5,然后你把它套到那个 array 上,就是 (one, two,three,five)。

       注意:Each index value can appear only once in the index set. This is an important concept to understand and is why you would not use index sets to store an arbitrary collection of integer values. To illustrate how this works, if you created an NSIndexSet object with the values 4, 5, 2, and 5, the resulting set would only have the values 4, 5, and 2 in it. Because index values are always maintained in sorted order, the actual order of the values when you created the set would be 2, 4, and then 5.

--方法、属性:常规的集合类操作,具体详见NSIndexSet.h;

--一些用法:

1.在NSArray中应用,例如:

    - (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes;   
    // ......  
    - (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
    - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
    - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;   

========================NSRange========================

--简介:A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.
--方法、属性:

1.location:The start index (0 is the first, as in C arrays);

2.length:The number of items in the range (can be 0);

========================NSCharacterSet========================

--简介:represents a set of Unicode-compliant characters。NSString NSScanner通常会用它对字符串进行分组等操作。

------------常用方法------------

--返回特定的字符集(字母数字的、十进制数、换行、空格等)

+ (id)alphanumericCharacterSet;
+ (id)decimalDigitCharacterSet;
……
+ (id)newlineCharacterSet NS_AVAILABLE(10_5, 2_0);
+ (id)whitespaceCharacterSet;
--用 其他对象创建characterSet(range、string、data、file)
+ (id)characterSetWithRange:(NSRange)aRange;
+ (id)characterSetWithCharactersInString:(NSString *)aString;
……
注意:方法中aRange代表Unicode中对应字符, 第一个字符是aRange.location,最后一个字符是aRange.location + aRange.length– 1。例如:
NSRange lcEnglishRange;
NSCharacterSet *lcEnglishLetters; 
lcEnglishRange.location = (unsigned int)'a';
lcEnglishRange.length = 26;
lcEnglishLetters = [NSCharacterSet characterSetWithRange:lcEnglishRange];




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值