ios - 无法使用 '[item]' 类型的索引为 'item' 类型的值添加下标

标签 ios arrays json swift

我正在尝试循环遍历 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)])), ...])

最佳答案

这部分:

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)
   }
}

将输出

现在 是 这 时间

代码:

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

没有意义。

关于ios - 无法使用 '[item]' 类型的索引为 'item' 类型的值添加下标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51092307/

相关文章:

javascript - Angular 重复-重复一次

ios - 来自服务器的 Restkit json 错误响应消息

arrays - Swift - 创建圆并添加到数组中

c# - JSON字符串格式到URL编码c#

javascript - 对每个数组索引值调用函数

ios - 在主数组的数组中查找 NSString

mysql - 访问数据库时保持代码模块化

objective-c - 如何通过应用内购买仅让付费成员(member)进入我的 iOS 应用程序的某些部分?

ios - 从 AppDelegate 呈现登录屏幕

ios - NSPredicate 过滤数组中对象中的数组