Issue Setting Products array... first Moltin App

products
swift

#1

Hi I am in the process of creating my first app with Moltin.It is able to connect with Moltin and retreive the data but I am having difficulty displaying it in the table view. I believe the error is somewhere in the setting of the products array. Please Help if you are able to, thankyou :slight_smile:

Remaining Lines:

products = productList.products.data as [String:AnyObject]

            let product = productList.products.first

            self.tableView.reloadData()
            
            
                }
            }
        }

Text in console:

Got products [Moltin.Product(id: “b2b5cf0d-4860-4862-baeb-c81ad8bfdd7a”, name: “jjj”, slug: “jj”, sku: “34343”, description: “jj”, commodityType: Moltin.Product.CommodityType.physical, dimensions: nil, weight: nil, files: [], collections: [], categories: [], brands: [], json: [“commodity_type”: physical, “name”: jjj, “sku”: 34343, “manage_stock”: 1, “id”: b2b5cf0d-4860-4862-baeb-c81ad8bfdd7a, “slug”: jj, “meta”: {
“display_price” = {
“with_tax” = {
amount = 1000;
currency = USD;
formatted = “$10.00”;
};
“without_tax” = {
amount = 1000;
currency = USD;
formatted = “$10.00”;
};
};
stock = {
availability = “in-stock”;
level = 10;
};
timestamps = {
“created_at” = “2018-03-22T05:19:27+00:00”;
“updated_at” = “2018-03-22T19:42:40+00:00”;
};
}, “status”: live, “description”: jj, “type”: product, “price”: <__NSSingleObjectArrayI 0x60000000e2c0>(
{
amount = 1000;
currency = USD;
“includes_tax” = 1;
}
)
, “relationships”: {
}], prices: [Moltin.Price(amount: 1000, currency: “USD”, includesTax: true, json: [“includes_tax”: 1, “currency”: USD, “amount”: 1000])], displayPriceWithTax: Optional(Moltin.DisplayPrice(amount: 1000, currency: “USD”, formatted: “$10.00”, json: [“currency”: USD, “formatted”: $10.00, “amount”: 1000])), displayPriceWithoutTax: Optional(Moltin.DisplayPrice(amount: 1000, currency: “USD”, formatted: “$10.00”, json: [“currency”: USD, “formatted”: $10.00, “amount”: 1000])), main_image: nil), Moltin.Product(id: “c016e5d4-5170-4ab5-8d45-4c3ef1a1189a”, name: “Rico”, slug: “RicoBeanie”, sku: “1341341343141341”, description: “100% Cotton \nExtremely warm \nMatching pom pom detail \nLightweight \nBreathable”, commodityType: Moltin.Product.CommodityType.physical, dimensions: nil, weight: nil, files: [], collections: [], categories: [], brands: [], json: [“commodity_type”: physical, “name”: Rico, “sku”: 1341341343141341, “manage_stock”: 1, “id”: c016e5d4-5170-4ab5-8d45-4c3ef1a1189a, “slug”: RicoBeanie, “meta”: {
“display_price” = {
“with_tax” = {
amount = 2000;
currency = USD;
formatted = “$20.00”;
};
“without_tax” = {
amount = 2000;
currency = USD;
formatted = “$20.00”;
};
};
stock = {
availability = “out-stock”;
level = 0;
};
timestamps = {
“created_at” = “2018-03-21T20:55:55+00:00”;
“updated_at” = “2018-03-22T05:25:53+00:00”;
};
}, “status”: live, “description”: 100% Cotton
Extremely warm
Matching pom pom detail
Lightweight
Breathable, “type”: product, “price”: <__NSSingleObjectArrayI 0x60000000e340>(
{
amount = 2000;
currency = USD;
“includes_tax” = 0;
}
)
, “relationships”: {
brands = {
data = (
{
id = “7eef0964-2774-43f7-bd8c-a0b37ed71533”;
type = brand;
}
);
};
categories = {
data = (
{
id = “2209cafd-e400-4174-86b4-80c6fed36c7d”;
type = category;
}
);
};
files = {
data = (
{
id = “80d46413-bdd0-47b2-a126-dd905509f334”;
type = file;
}
);
};
“main_image” = {
data = {
id = “5a78170f-3027-4af5-8adb-79f6fad27bb2”;
type = “main_image”;
};
};
}], prices: [Moltin.Price(amount: 2000, currency: “USD”, includesTax: false, json: [“includes_tax”: 0, “currency”: USD, “amount”: 2000])], displayPriceWithTax: Optional(Moltin.DisplayPrice(amount: 2000, currency: “USD”, formatted: “$20.00”, json: [“currency”: USD, “formatted”: $20.00, “amount”: 2000])), displayPriceWithoutTax: Optional(Moltin.DisplayPrice(amount: 2000, currency: “USD”, formatted: “$20.00”, json: [“currency”: USD, “formatted”: $20.00, “amount”: 2000])), main_image: nil)]
2018-03-22 13:51:20.697342-0700 Silent Crowd Shopping App[38541:6595050] *** Assertion failure in -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:7880
2018-03-22 13:51:20.731460-0700 Silent Crowd Shopping App[38541:6595050] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard’
*** First throw call stack:
(
0 CoreFoundation 0x000000010812712b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001077bbf41 objc_exception_throw + 48
2 CoreFoundation 0x000000010812c2f2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010725cd69 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x00000001086eae51 -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:] + 890
5 UIKit 0x00000001086eaaa3 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 89
6 Silent Crowd Shopping App 0x0000000106ba9aa2 _T025Silent_Crowd_Shopping_App20MasterViewControllerC05tableF0So07UITableF4CellCSo0iF0C_10Foundation9IndexPathV12cellForRowAttF + 274
7 Silent Crowd Shopping App 0x0000000106baa0ac _T025Silent_Crowd_Shopping_App20MasterViewControllerC05tableF0So07UITableF4CellCSo0iF0C_10Foundation9IndexPathV12cellForRowAttFTo + 92
8 UIKit 0x0000000108706484 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 778
9 UIKit 0x0000000108706a2a -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
10 UIKit 0x00000001086cc1f6 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3031
11 UIKit 0x00000001086ee2e6 -[UITableView layoutSubviews] + 176
12 UIKit 0x0000000108676a6d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1439
13 QuartzCore 0x000000011198b61c -[CALayer layoutSublayers] + 159
14 QuartzCore 0x000000011198f7ad _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401
15 QuartzCore 0x000000011191686c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 364
16 QuartzCore 0x0000000111943946 _ZN2CA11Transaction6commitEv + 500
17 QuartzCore 0x0000000111944694 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
18 CoreFoundation 0x00000001080c9c07 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23
19 CoreFoundation 0x00000001080c9b5e __CFRunLoopDoObservers + 430
20 CoreFoundation 0x00000001080ae124 __CFRunLoopRun + 1572
21 CoreFoundation 0x00000001080ad889 CFRunLoopRunSpecific + 409
22 GraphicsServices 0x00000001104a39c6 GSEventRunModal + 62
23 UIKit 0x00000001085a55d6 UIApplicationMain + 159
24 Silent Crowd Shopping App 0x0000000106bac9c7 main + 55
25 libdyld.dylib 0x000000010c6dad81 start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException


#2

More Photo


#3

I have also tried changing line 45 to:

            self.products = productList.products as [AnyObject]

and results in this error:


#4

Hey!

Looks like there’s a few issues here, but I’m not sure if they’re related directly to Moltin.

Your first screenshot (products = productList.products.data as [String:AnyObject]) is due to an issue where data is not a parameter on products. If you look at the type of productList.products you’ll find it’s [Product] - an array of Product's. You’ll want to update your line 16 in that file to var products = [Product]() also.

Your next error looks to be an issue with registering cell’s for your table view - Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard.

There are a few ways to register cells for table view’s, you can take a look at this StackOverflow post for more answers https://stackoverflow.com/a/43400993 or to find out more check out the Apple documentation, such as https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html

Hope this helps!
Craig


#5

Yes Craig, this helps very much, thankyou!

Reference to

our first screenshot (products = productList.products.data as [String:AnyObject]) is due to an issue where data is not a parameter on products. If you look at the type of productList.products you’ll find it’s [Product] - an array of Product’s.

I’m struggling to understand the difference of when I should use Product vs. product. So far I’ve used Product twice.(till i add 1 more later)

New Code:

import UIKit
import Moltin
class MasterViewController: UITableViewController {

var detailViewController: DetailViewController? = nil
var products = [Product]()
let reference = UUID().uuidString


override func viewDidLoad() {
    super.viewDidLoad()
    
    
    // Do any additional setup after loading the view, typically from a nib.
    
    Moltin.clientID = ("je5XmFLELNSEIPD6CDssgiIk8pV6gRUIjPItEpNqqG")
    
    
    
    if let split = splitViewController {
        let controllers = split.viewControllers
        detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    }
    
    Moltin.product.list() { result in
        switch result {
            
        case .failure(let error):
            print("Failed to get products:\n\(error)")
            
            
        case .success(let productList):
            print("Got products \(productList.products)")
            
            self.products = productList.products as [Product]
            
            self.tableView.reloadData()
                }
            }
        }

^^ I changed line 45 to
self.products = productList.products

I also fixed line 16 also var products = Product and then lines 67 gives me an error.

@objc
func insertNewObject(_ sender: Any) {
products.insert(NSDate(), at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)

The error is "cannot convert value type of ‘ns date’ to expected argument type ‘Product’.

I changed NSDate to Product.

and the new error is

Missing argument for parameter ‘json’ in call.

Code around those lines:

@objc
func insertNewObject(_ sender: Any) {
products.insert(Product(), at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}


#6

Hey!

Are you using the Master/Detail application that XCode sets up for you when you create a new project?

IIRC it looks like you’ve got the basic application where there’s a plus button in the navigation bar, and when you press it, that insertNewObject function is called?

You likely don’t need that function at all, so you can get rid of it if you’ve got a table view and don’t need to add new products on button tap.


#7

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.