2017-10-11 17 views
-1

我想下标包含在堆栈中的字典,但每当我做我得到一个错误,如“无法下标类型'[Dictionary<UInt64, UInt 64>]' with an index of type 'UInt64'. 值下标字典堆栈被声明为:堆栈中的迅速

var cost : Stack<Dictionary<UInt64, UInt64>>? = nil 

而这给我的麻烦线路:

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

我试图写它了一堆不同的方式,但他们都给予了同样的错误 编辑:这是栈代码

struct Stack<T> { 
    var items = [T]() 
    mutating func push(_ item: T) { 
     items.append(item) 
    } 
    mutating func pop() -> T? { 
     return items.removeLast() 
    } 
} 

回答

0

你宣布var items = [T](),所以items是一个Array<T>bArray<T>。数组下标是Int类型,而不是UInt64。试试这个:

+0

项目不是可选的,所以我把它改成'如果= {零'和它的工作(x.cost .items [INT(指数)?!)。非常感谢,罗布。 –