Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Arrays ClosedInterval<;字符串>;用Swift[串]_Arrays_Swift_String - Fatal编程技术网

Arrays ClosedInterval<;字符串>;用Swift[串]

Arrays ClosedInterval<;字符串>;用Swift[串],arrays,swift,string,Arrays,Swift,String,为什么: let i = 1...100 let s = [Int](i) 屈服 [1, 2, 3, ..., 100] Cannot invoke initializer for type '[String]' with an argument list of type '(ClosedInterval<String>)' true 但是 屈服 [1, 2, 3, ..., 100] Cannot invoke initializer for type '[String]

为什么:

let i = 1...100
let s = [Int](i)
屈服

[1, 2, 3, ..., 100]
Cannot invoke initializer for type '[String]' with an argument list of type '(ClosedInterval<String>)'
true
但是

屈服

[1, 2, 3, ..., 100]
Cannot invoke initializer for type '[String]' with an argument list of type '(ClosedInterval<String>)'
true
屈服

[1, 2, 3, ..., 100]
Cannot invoke initializer for type '[String]' with an argument list of type '(ClosedInterval<String>)'
true

Int
符合
ForwardIndexType
,因此在

let i = 1...100
let i = "a"..."z"
是操作员

public func ...<Pos : ForwardIndexType where Pos : Comparable>(start: Pos, end: Pos) -> Range<Pos>
public func ...<Bound : Comparable>(start: Bound, end: Bound) -> ClosedInterval<Bound>
是操作员

public func ...<Pos : ForwardIndexType where Pos : Comparable>(start: Pos, end: Pos) -> Range<Pos>
public func ...<Bound : Comparable>(start: Bound, end: Bound) -> ClosedInterval<Bound>
产生
true

在Swift 3中,范围和间隔类型已重命名并 重组:

"1" ... "100"  // CountableClosedRange<Int>
"a" ... "z"    // ClosedRange<String>
结果是

["a", "b", "c", ... , "z"]
作为
字符的数组
字符串的数组