[IOS] NSIndexPath , NSIndexSet , NSRange 之间的区别比较

The NSIndexSet class represents an immutable collection of unique unsigned integers, known as indexes because of the way they are used. This collection is referred to as a index set.

You use index sets in your code to store indexes into some other data structure. For example, given an NSArray object, you could use an index set to identify a subset of objects in that array.

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.

In most cases, using an index set is more efficient than storing a collection of individual integers. Internally, the NSIndexSet class represents indexes using ranges. For maximum performance and efficiency, overlapping ranges in an index set are automatically coalesced—that is, ranges merge rather than overlap. Thus, the more contiguous the indexes in the set, the fewer ranges are required to specify those indexes.

The designated initializers of the NSIndexSet class are: initWithIndexesInRange: and initWithIndexSet:.

You must not subclass the NSIndexSet class.

The mutable subclass of NSIndexSet is NSMutableIndexSet.

====================================================================

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.

Each index in an index path represents the index into an array of children from one node in the tree to another, deeper, node. For example, the index path 1.4.3.2 specifies the path shown in Figure 1.

====================================================================
NSRange
A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.

typedef struct _NSRange {
      NSUInteger location;
      NSUInteger length;
} NSRange;
Fields
location
The start index (0 is the first, as in C arrays).
length
The number of items in the range (can be 0).

======================================================
NSIndexSet 用来让你从某个 data structure 里面提取一部分东西出来成为一个新的东西。
比如你有一个 NSArray, 里面是
(one, two, three, four, five)
然后你造了个 indexSet 的内容是 1,2,3,5
然后你把它套到那个 array 上,就是 (one, two,three,five)
它是这么说的,具体看看apple的文档

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

======================================================
NSRange 就是 从 x 到 y
比如 3 到 6

比如你有一个 NSString, 内容是 "go home and eat"
你就可以拿这个range截取这个 NSString, 3-6 就是 “home”

它是这么说的,具体看看apple的文档

转载于:https://www.cnblogs.com/iphone520/archive/2012/02/01/2334321.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值