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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.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 在堆栈中订阅词典_Swift_Swift Subscript - Fatal编程技术网

Swift 在堆栈中订阅词典

Swift 在堆栈中订阅词典,swift,swift-subscript,Swift,Swift Subscript,我正在尝试为堆栈中包含的字典下标,但每当我下标时,都会出现一个错误,如“无法为类型为,[字典]”的值下标,索引类型为“UInt64”。 堆栈声明为: var cost : Stack<Dictionary<UInt64, UInt64>>? = nil 我试过用很多不同的方法来写,但它们都给出了相同的错误。 编辑:这是堆栈代码 struct Stack<T> { var items = [T]() mutating func push(_ i

我正在尝试为堆栈中包含的字典下标,但每当我下标时,都会出现一个错误,如“无法为类型为
,[字典]”的值下标,索引类型为“UInt64”。
堆栈声明为:

var cost : Stack<Dictionary<UInt64, UInt64>>? = nil
我试过用很多不同的方法来写,但它们都给出了相同的错误。 编辑:这是堆栈代码

struct Stack<T> {
    var items = [T]()
    mutating func push(_ item: T) {
        items.append(item)
    }
    mutating func pop() -> T? {
        return items.removeLast()
    }
}
struct堆栈{
变量项=[T]()
变异函数推送(u项:T){
items.append(项目)
}
变异函数pop()->T{
return items.removeLast()
}
}

您声明了
var items=[T]()
,因此
items
是一个
数组
b
也是
数组
。数组下标的类型是
Int
,而不是
UInt64
。请尝试以下操作:

if let b = x.cost?.items, let _ = b[Int(index)] {

项目不是可选的,所以我将其更改为
if(x.cost?.items[Int(index)]!=nil{
,并且成功了。非常感谢你,Rob。
if let b = x.cost?.items, let _ = b[Int(index)] {