Swift Compiler error Type '[AnyHashable : Any]?' has no subscript members


#1

Hello there

I am doing the beginner tutorial Creating a mobile store and I think this is done by older version.
The Moltin.sharedInstance().product.listing function now is with AnyHashable instead of older Any Object!

My code:

I don’t know whats an AnyHashable. Can somebody explain how to update my project version with the terminal command pod install , or what I have to do to solve this issue ?
My version now is 9.0 noted in the Podfile.

Thank you in advance
Christoph


#2

Hi Christoph,

Yeah, looks like you’re on the older version of the SDK here. To install the latest version, you’ve got to grab it from the branch in your Podfile. That looks like this:

pod 'Moltin', :git => 'https://github.com/moltin/ios-sdk.git', :branch => 'v2'

This will install the latest version of the SDK, so your code that calls out to Moltin will be slightly different too (note, no shared instance):

Moltin.products.list(...

Hope this helps,
Craig


#3

Thank you for your information, worked well! Now I got error Moltin/Moltin.h’ file not found … should I create the hole project new?


#4

Do you have a bridging header in the project anywhere? You won’t need any references to Moltin.h anymore if you have those anywhere, such as:

#import <Moltin/Moltin.h>;

#5

Okay everything works fine thank you!

Is there any example how to add the products in the iOS App listing? I tried to do the Getting started Swift, but there are some errors and I don’t know : /

Would be glad to get example how to do : )

Many thanks!!!


#6

Hey Christoph,

Due to using blocks (callbacks/closures), your productList variable will only be available within the completion block of the list() method, specifically within the case block of the switch, lines 30 & 31 in your screenshot.

Generally what you’d do here is fire off some function from within your success case, such as showing the products in a UITableView, optionally storing the products that have been retrieved in some way:

case .success(let productList):
    // Store products
    self.products = productList
    // Reload table view
    self.tableView.reloadData()

In your case, you could create a function that would do what you’re doing in lines 35 - 44, and call that function from within that success case.

Hope this helps,
Craig


#7

Thank you very much for great explanation!!!

Can you also explain how to define cartID ?

Great work thank you in advance : )


#8

The cart reference is simply a UUID that you store somewhere to keep track of a single cart, so you could just set the reference to let reference = UUID().uuidString. If a cart does not exist for a reference, then it’ll create a new cart for you.

There is also Moltin.cart.getNew, this does essentially the same thing, but returns you a new Cart object from the API.


#9

So now I got error with the productID and I also tried to print out the number of products (should be two from dashboard) but Xcode print out 0 products in list.

Can you help me to get the productID and get them in tableView?

Thank you!!!


#10

The reason this happens is due to productList.products.first returning an optional value. (https://developer.apple.com/documentation/swift/optional)

This means that product could either be the first product in the array, or code was not able to get a product from the list, so it’s nil.

Generally what I’d recommend here is to use a guard let statement, which validates that the product is not nil before continuing on, that will look something like this:

guard let product = productList.products.first else {
    // Could not get a product, just return out of the function
    return
}

// `product` is no longer optional because it's been validated in the guard let
Moltin.card.add(itemWithProductID: product.id ...

Hope this helps,
Craig