cannot subscript a value of type '[item]' with an index of type 'item'
cannot subscript a value of type '[item]' with an index of type 'item'
I'm trying to loop trough an array within a JSON struct, but whatever I do i'm getting the error "Cannot subscript a value of type '[item]' with an index of type 'item'. I can print it whenever I call upon each index separately:
self.socialTitle = (storeSocialContext.items?[1].metatags?.first?.title)!
print(self.socialTitle)
Which gives me back a string, how i want it. But I want the strings of each index, for the title.
This is the loop which gives the error:
var anArray = storeSocialContext.items!
for socialIndex in anArray {
if anArray[socialIndex] != nil {
}
}
And this is the struct:
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"
}
}
And this is a snipped of the data whenever I fetch and print the JSONdata in the console:
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, I edited it. I messed up the variables while typing up the question. Hope it's more clear now. Let me know if not.
– PR10
2 days ago
A for-in loop iterates over the items in the array, not the index. So unless you need the index itself for something, you already have your item and don't need to subscript anything. See docs.swift.org/swift-book/LanguageGuide/ControlFlow.html
– John Montgomery
2 days ago
Apart from the issue the
nil
check is pointless. The array doesn't contain any optionals.– vadian
2 days ago
nil
1 Answer
1
This part:
for socialIndex in anArray {
if anArray[socialIndex] != nil {
//do stuff
}
}
Doesn't make any sense.
If you have a type Thing
, and an array
Thing
var array: [Thing] = [thing1, thing2, thing3]
and you use the code for thing in array { //your code here }
for thing in array { //your code here }
then the variable thing
contains elements of your array, and is of the type Thing
thing
Thing
In your code for socialIndex in anArray
your variable socialIndex
will contain elements from your array, and be of the type of those array elements.
for socialIndex in anArray
socialIndex
Thus the bit where you try to index into anArray: anArray[socialIndex]
is not valid.
anArray[socialIndex]
You need to index into the array using Int indexes, and you've already extracted an element from the array, so there's no point.
This code:
let strings: [String?] = ["now", "is", "the", nil, "time"]
for string in strings {
if let aString = string {
print(aString)
}
}
Will output
now
is
the
time
now
is
the
time
And the code:
for string in strings {
if strings[string] != nil { //This makes no sense. `string` is of type String
}
}
Doesn't make sense.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is
socialTry
? Why is your array ofItem
assigned to a variable namedsocialItem
instead ofsocialItems
? In yourfor
loop, why is a singleItem
assigned to a variable namedsocialIndex
? Better variable naming will make your code much easier to read and it will make your issues easier to solve.– rmaddy
2 days ago