This demo app uses JSON and CoreData to list and display products from Walmart's server. Written in Swift.
-
Written in Swift 4.
-
Uses CoreData to cache responses from the server to speed up processing.
-
Universal app: supports iPhone & iPad.
-
On the detail view, you can swipe left or right to view other items listed in the table view.
Some of the project files are auto-generated. If you receive build errors for missing classes, simply open the WalMartProductsSwift.xcdatamodeld file to make xCode auto-generate the relevant files. Then rebuild the project.
Several custom classes appear in this project in order to converse with the Walmart server. By group:
These classes handle the interaction between the app and the Walmart server.
Product.swift: This class is an extension to the Product class generated by xCode for use with CoreData. It handles the parsing of JSON data into developer-friendly objects. If there is a parsing error, the class will generate a ProductJSONParsingError error or NSError and provide feedback about what happened.
ProductPageRequest.swift: This class handles the JSON web request to the Walmart server. The requestPage method takes care of setting up the request and sending it to the server. The results of the request are processed by the caller's supplied closure. A successful response will parse the JSON data into Product objects before calling the closure. If there was an error, then it is returned in the closure in the form of a ProductRequestError or NSError. A url response status code is also returned for processing.
These unit tests are designed to test core functionality such as correctly parsing valid JSON into a Product object, ensuring that invalid JSON generates the proper error, making sure the CoreData entry is set up properly, and ensuring that the web request is formulated correctly.