Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 为什么IndexSet可以用负数初始化?_Swift - Fatal编程技术网

Swift 为什么IndexSet可以用负数初始化?

Swift 为什么IndexSet可以用负数初始化?,swift,Swift,about IndexSet表示: 有效整数值的范围为0..这是一个错误。它来自于这样一个事实:索引集的初始值设定项是不可失败的。是标准库中定义的初始值设定项: /// Initialize an `IndexSet` with a single integer. public init(integer: Element) { _handle = _MutablePairHandle(NSIndexSet(index: integer), copying: false) } 为了与定义

about IndexSet表示:


有效整数值的范围为0..这是一个错误。它来自于这样一个事实:索引集的初始值设定项是不可失败的。是标准库中定义的初始值设定项:

/// Initialize an `IndexSet` with a single integer.
public init(integer: Element) {
    _handle = _MutablePairHandle(NSIndexSet(index: integer), copying: false)
}
为了与定义保持一致,应该使用无符号整数:


有效整数值的范围是0。对于一个似乎没有限制的范围,这可以让indexSet=IndexSetintegersIn:-100。在我看来这是一个bug。如果您执行var indexSet=indexsetinger:-1;indexSet.insert0则indexSet.count为2,但对于indexSet中的x,{printx}不打印任何内容。可能只是我。。。但该文档读起来像0以下的任何内容都是未定义的行为。它可能会崩溃,也可能会好起来。。。所有这些都是可接受的初始化器不应该失败。不允许使用负索引并插入它们,或者应该允许并处理它们。使用扩展的变通方法很好,但是@AirspeedVelocity所说的初始化步骤是有道理的。所以,我也认为这是一个错误。@pacification我开始回答说这是一个错误。初始值设定项在设计上是不会失败的,它应该使用uint而不是int。但是int非常常见,这将是一种糟糕的语言设计,因为在IndexSet初始化时,转换到uint是无处不在的。
/// Initialize an `IndexSet` with a single integer.
public init(integer: Element) {
    _handle = _MutablePairHandle(NSIndexSet(index: integer), copying: false)
}
extension IndexSet {
    public init(unsignedInt: UInt) {
        self = IndexSet(integer: Int(unsignedInt))
    }
}
IndexSet(unsignedInt: -1) //Negative integer '-1' overflows when stored into unsigned type 'UInt'