2016-04-26 52 views
0

我得到这个错误:模糊使用下标,并且我不知道如何解决它。能否请你帮忙 ? 第一个是在该行:Swift,错误:模糊使用下标

next = next[sub]

下面是代码:

public subscript(path: [SubscriptType]) -> JSON { 
    get { 
     if path.count == 0 { 
      return JSON.nullJSON 
     } 

     var next = self 
     for sub in path { 
      next = next[sub] 
     } 
     return next 
    } 
    set { 

     switch path.count { 
     case 0: return 
     case 1: self[path[0]] = newValue 
     default: 
      var last = newValue 
      var newPath = path 
      newPath.removeLast() 
      for sub in Array(path.reverse()) { 
       var previousLast = self[newPath] 
       previousLast[sub] = last 
       last = previousLast 
       if newPath.count <= 1 { 
        break 
       } 
       newPath.removeLast() 
      } 
      self[newPath[0]] = last 
     } 
    } 
} 

非常感谢你,

+0

它是关键订阅还是索引订阅?这就是*含糊*的含义。 – vadian

回答

0

你需要告诉编译数据类型self是,像:

var next = self as? [String]

var next = self as? [String:AnyObject]

如果你不知道你会想去做

if let next = self as? [String] 
    { // foo } 
else if let next = self as? [String:AnyObject] 
    { // bar } 

分支条件(我不知道究竟是什么在这里型self,您可能需要其他类型的比[String][String:AnyObject])。