Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法为类型为';[项目]和#x27;索引类型为';项目';_Ios_Arrays_Json_Swift - Fatal编程技术网

Ios 无法为类型为';[项目]和#x27;索引类型为';项目';

Ios 无法为类型为';[项目]和#x27;索引类型为';项目';,ios,arrays,json,swift,Ios,Arrays,Json,Swift,我试图通过JSON结构中的数组进行循环,但无论我做什么,都会出现错误“无法将“[item]”类型的值下标为“item”类型的索引。每当我单独调用每个索引时,我都可以打印它: self.socialTitle = (storeSocialContext.items?[1].metatags?.first?.title)! print(self.socialTitle) 这给了我一个字符串,我想要它的方式。但是我想要每个索引的字符串,作为标题。 这是给出错误的循环: var anArray =

我试图通过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
    }
}
没有任何意义

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

var array: [Thing] = [thing1, thing2, thing3]
您将代码
用于数组中的东西{//your code here}

然后变量
thing
包含数组的元素,类型为
thing

在数组中的socialIndex的代码
变量
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
   }
}

没有意义。

什么是
socialTry
?为什么将
项的数组分配给名为
socialItem
的变量而不是
socialItems
?在
for
循环中,为什么将单个
分配给名为
socialIndex
的变量?更好的变量命名将使您的代码更容易阅读,也更容易解决您的问题。Tnx,我编辑了它。我在键入问题时弄乱了变量。希望它现在更清楚。如果不清楚,请告诉我。for-in循环迭代数组中的项,而不是索引。因此,除非您需要索引本身,否则您已经有了您的项,并且不需要下标任何内容。除此之外,
nil
检查没有意义。数组不包含任何选项。