Xcode NSRange变量能否包含多个范围?

Xcode NSRange变量能否包含多个范围?,xcode,cocoa,swift,Xcode,Cocoa,Swift,NSRange变量是否可能包含多个范围?比如: var multipleRanges: NSRange = [NSMakeRange(0, 2), NSMakeRange(10, 1), ...] 或者多个范围可能有另一个变量类型?单个NSRange变量可以容纳单个范围。如果需要存储多个范围,请创建一个数组: var multipleRanges: [NSRange] = [NSMakeRange(0, 2), NSMakeRange(10, 1)] //

NSRange变量是否可能包含多个范围?比如:

 var multipleRanges: NSRange = [NSMakeRange(0, 2), NSMakeRange(10, 1), ...]
或者多个范围可能有另一个变量类型?

单个NSRange变量可以容纳单个范围。如果需要存储多个范围,请创建一个数组:

var multipleRanges: [NSRange] = [NSMakeRange(0, 2), NSMakeRange(10, 1)]
//                  ^       ^
//                  |       |
// This tells Swift that you are declaring an array, and that array elements
// are of NSRange type.
您还可以省略类型,让编译器为您推断:

// This is the same declaration as above, but now the type of array element
// is specified implicitly through the type of initializer elements:
var multipleRanges = [NSMakeRange(0, 2), NSMakeRange(10, 1)]
单个NSRange变量可以容纳单个范围。如果需要存储多个范围,请创建一个数组:

var multipleRanges: [NSRange] = [NSMakeRange(0, 2), NSMakeRange(10, 1)]
//                  ^       ^
//                  |       |
// This tells Swift that you are declaring an array, and that array elements
// are of NSRange type.
您还可以省略类型,让编译器为您推断:

// This is the same declaration as above, but now the type of array element
// is specified implicitly through the type of initializer elements:
var multipleRanges = [NSMakeRange(0, 2), NSMakeRange(10, 1)]
或者可能有另一个变量类型用于多个范围

是,NSMutableIndexSet将唯一无符号整数的集合存储为范围序列

示例:创建可变索引集并添加两个范围和一个索引:

let indexSet = NSMutableIndexSet()
indexSet.addIndexesInRange(NSMakeRange(0, 2))
indexSet.addIndexesInRange(NSMakeRange(10, 3))
indexSet.addIndex(5)
println(indexSet)
// <NSMutableIndexSet: 0x10050a510>[number of indexes: 6 (in 3 ranges), indexes: (0-1 5 10-12)]
列举所有范围:

indexSet.enumerateRangesUsingBlock { (range, stop) -> Void in
    println(range)
}
// Output: (0,2) (5,1) (10,3)
测试成员:

if indexSet.containsIndex(11) {
    // ...
}
但请注意,NSIndexSet表示一个集合,即没有重复的元素, 元素的顺序并不重要。这可能是也可能不是 根据您的需要,您可以使用它。例如:

let indexSet = NSMutableIndexSet()
indexSet.addIndexesInRange(NSMakeRange(0, 4))
indexSet.addIndexesInRange(NSMakeRange(2, 4))
indexSet.enumerateRangesUsingBlock { (range, stop) -> Void in
    println(range)
}
// Output: (0,6)
或者可能有另一个变量类型用于多个范围

是,NSMutableIndexSet将唯一无符号整数的集合存储为范围序列

示例:创建可变索引集并添加两个范围和一个索引:

let indexSet = NSMutableIndexSet()
indexSet.addIndexesInRange(NSMakeRange(0, 2))
indexSet.addIndexesInRange(NSMakeRange(10, 3))
indexSet.addIndex(5)
println(indexSet)
// <NSMutableIndexSet: 0x10050a510>[number of indexes: 6 (in 3 ranges), indexes: (0-1 5 10-12)]
列举所有范围:

indexSet.enumerateRangesUsingBlock { (range, stop) -> Void in
    println(range)
}
// Output: (0,2) (5,1) (10,3)
测试成员:

if indexSet.containsIndex(11) {
    // ...
}
但请注意,NSIndexSet表示一个集合,即没有重复的元素, 元素的顺序并不重要。这可能是也可能不是 根据您的需要,您可以使用它。例如:

let indexSet = NSMutableIndexSet()
indexSet.addIndexesInRange(NSMakeRange(0, 4))
indexSet.addIndexesInRange(NSMakeRange(2, 4))
indexSet.enumerateRangesUsingBlock { (range, stop) -> Void in
    println(range)
}
// Output: (0,6)

您还可以省略类型注释并让编译器推断类型:var multipleRanges=[NSMakeRange0,2,NSMakeRange10,1]。您还可以省略类型注释并让编译器推断类型:var multipleRanges=[NSMakeRange0,2,NSMakeRange10,1]。