【问题标题】:cannot subscript a value of type '[item]' with an index of type 'item'不能用“item”类型的索引为“[item]”类型的值下标
【发布时间】:2018-06-28 23:11:52
【问题描述】:

我正在尝试循环遍历 JSON 结构中的数组,但无论我做什么,我都会收到错误“无法使用 'item' 类型的索引下标 '[item]' 类型的值。我可以每当我分别调用每个索引时打印它:

self.socialTitle = (storeSocialContext.items?[1].metatags?.first?.title)!
print(self.socialTitle)

这给了我一个字符串,我想要它。但我想要每个索引的字符串,作为标题。 这是给出错误的循环:

 var anArray = storeSocialContext.items!

                for socialIndex in anArray {
                    if anArray[socialIndex] != nil {

                    }

                }

这是结构:

struct StoreSocialContext: Decodable
{
    var items: [Item]?
}

struct Item: Decodable
{
    var metatags: [enclosedTags]?

    enum CodingKeys : String, CodingKey
    {
        case pagemap
    }

    enum PageMapKeys: String, CodingKey
    {
        case metatags
    }

    init(from decoder: Decoder) throws
    {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        let pagemap = try values.nestedContainer(keyedBy: PageMapKeys.self, forKey: .pagemap)
        metatags = try pagemap.decode([enclosedTags].self, forKey: .metatags)
    }
}

struct enclosedTags: Decodable
{
    let image: String?
    let title: String?
    let description: String?
    let siteName: String?

    private enum CodingKeys : String, CodingKey
    {
        case image = "og:image", title = "og:title", description = "og:description", siteName = "og:site_name"
    }
}

这是我在控制台中获取和打印 JSONdata 时的数据片段:

Optional([GetContext.Item(metatags: Optional([GetContext.enclosedTags(image: 
nil, title: nil, description: nil, siteName: nil)])), GetContext.Item(metatags: 
Optional([GetContext.enclosedTags(image: Optional("https://www.merriam- 
webster.com/assets/mw/static/social-media-share/mw-logo-245x245@1x.png"), 
title: Optional("Definition of BEST"), description: Optional("excelling all 
others; most productive of good : offering or producing the greatest advantage, 
utility, or satisfaction; most, largest… See the full definition"), siteName: 
nil)])), ...])

【问题讨论】:

  • 什么是socialTry?为什么将Item 数组分配给名为socialItem 的变量而不是socialItems?在您的for 循环中,为什​​么将单个Item 分配给名为socialIndex 的变量?更好的变量命名将使您的代码更易于阅读,并使您的问题更易于解决。
  • Tnx,我编辑了它。我在输入问题时弄乱了变量。希望现在更清楚了。如果没有,请告诉我。
  • for-in 循环遍历数组中的项目,而不是索引。因此,除非您需要索引本身的某些内容,否则您已经拥有您的项目并且不需要下标任何内容。见docs.swift.org/swift-book/LanguageGuide/ControlFlow.html
  • 除了问题之外,nil 检查毫无意义。该数组不包含任何选项。

标签: ios arrays json swift


【解决方案1】:

这部分:

for socialIndex in anArray {
    if anArray[socialIndex] != nil {
        //do stuff
    }
}

没有任何意义。

如果你有一个类型Thing,还有一个数组

var array: [Thing] = [thing1, thing2, thing3]

你使用代码for thing in array { //your code here }

那么变量thing 包含你的数组的元素,并且是Thing 类型的

在您的代码for socialIndex in anArray 中,您的变量socialIndex 将包含您数组中的元素,并且属于这些数组元素的类型。

因此,您尝试索引到 anArray 的位:anArray[socialIndex] 无效。

您需要使用 Int 索引对数组进行索引,并且您已经从数组中提取了一个元素,所以没有意义。

这段代码:

let strings: [String?] = ["now", "is", "the", nil, "time"]

for string in strings {
   if let aString = string {
       print(aString)
   }
}

会输出

now is the time

还有代码:

for string in strings {
   if strings[string] != nil { //This makes no sense. `string` is of type String
   }
}

没有意义。

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 2019-11-19
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多