【问题标题】:SourceKitService Crash in Xcode 7.3 with Dictionaries in for loopXcode 7.3 中的 SourceKitService 崩溃,字典位于 for 循环中
【发布时间】:2016-05-07 14:08:41
【问题描述】:

也许正如我已经看到的那样,这已经说过了,但似乎我找不到适合我的正确答案。我有一个由字典组成的数组。我正在使用 swift 2.2,并且正在尝试使用“for”循环访问这些字典。但是当我试图访问它们时,我得到了 SourceKitService 崩溃。以下是我的代码:

    for index in productArray{
        var Dict = index["id"]
    }

当我输入并进入重点时:index[" 我收到错误并且 xcode 崩溃。我在我的项目中使用其他字典,但它们工作正常。也许我的代码有问题。我不知道。

productArray 是一个 NSMutableArray。

我通过添加以下方法字典创建了 productArray:

productArray.addObject(dictionary)

这是 productArray 数组的示例。

(
 { id = 1; price = "2.00"; "product_name" = "A"; },
 { id = 2; price = "2.00"; "product_name" = "B"; }
)

我还尝试了以下方法:

                for index in 1...productArray.count{
                var dict = productArray.objectAtIndex(index)
                print(dict[
            }

但是当我达到这一点时,它又一次崩溃了。当我尝试输入 "" 时它会崩溃。

如果有人可以提供帮助,我们将不胜感激。谢谢你。

【问题讨论】:

  • 如果您的productArray 真的 是一个字典数组,那么您的代码就可以工作。请edit 向我们展示productArray 的打印件(或者至少是Xcode 所说的类型)。

标签: ios xcode swift dictionary


【解决方案1】:

我通过在dict的初始化后面添加以下内容解决了这个问题。

首先我有:

var dict = productArray.objectAtIndex(index)

然后:

var dict = productArray.objectAtIndex(index) as! [String:String]

这解决了我的问题并且工作正常。

【讨论】:

  • 我的问题的解决方案是我找到的。问题与代码结构的具体方式有关。如果我必须将整个代码更改为其他代码,这将不是我问题的答案。谢谢。
  • 我投了赞成票!自从你回答以来,它已经从一开始就被投票了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多