I am stuck in situation where I could not able to find solution. Seen many similar question but could't able to figure it out.
So I have one View Controller which shows table view list for two types
1) Sales
2) Purchase
And I have decided to use same View Controller for both
enum SalesRowType {
case salesOrderBooking
case salesInvoicing
case salesGrossContribution
}
enum PurchaseRowType {
case purchaseOrders
case amended_non_amended
case pending_grn_po
}
and
// BASE PROTOCOL
// ListRowType -> has title,image property
protocol HomeSubItemType:ListRowType {
associatedtype RowType
var type:RowType {get}
}
// PURCHASE ITEMS
struct PurchaseSubItem: HomeSubItemType {
typealias RowType = PurchaseRowType
var image: UIImage
var title: String
var type: PurchaseRowType
}
// SALES ITEMS
struct SalesSubItem : HomeSubItemType {
var image:UIImage
var title:String
var type:SalesRowType
}
In My View Controller I want to create array as per sales and purchase
if let type = type {
switch type {
case .purchase:
self.title = "Purchase"
self.itemList = [
PurchaseSubItem(image: UIImage(named: "purchase-orders")!, title: "Purchase Orders", type: .purchaseOrders),
PurchaseSubItem(image: UIImage(named: "amended-non")!, title: "Amended/NON-Amended-UNAutho-PO", type: .purchaseOrders),
PurchaseSubItem(image: UIImage(named: "purchase-pending")!, title: "Pending GRN PO", type: .purchaseOrders)]
case .sales:
self.title = "Sales"
self.itemList =
[
SalesSubItem(image: UIImage(named: "sales-order-booking")!, title: "Sales Order Booking", type: .salesOrderBooking),
SalesSubItem(image: UIImage(named: "sales-invoice")!, title: "Sales Invoicing", type: .salesInvoicing),
SalesSubItem(image: UIImage(named: "sale-gross")!, title: "Sales Gross Contribution", type: .salesGrossContribution)]
default:
assertionFailure("Only Purchase and sales are handle here")
}
}
But I am not able to declare array
var itemList = [HomeSubItemType]()
//ERROR HERE
Protocol 'HomeSubItemType' can only be used as a generic constraint because it has Self or associated type requirements
Please help me to fix this. Thanks in advance