基于Enum和Struct的静态与动态章节建模 - Swift 4
Hey there! Let's work through how to adjust your Swift models to fit the backend data you're receiving. The core issue here is that your Item.Pricing case needs to carry an array of data instead of being a plain identifier—we can fix this easily with Swift enum associated values, plus a few small tweaks to map the backend response properly.
Step 1: Update the Item Enum with Associated Values
First, we'll modify the Item enum to attach actual data to each case (since right now they're just labels). For the Pricing case, we'll add an associated value that holds an array of pricing-specific models. We'll also define a PricingModel to match the structure of the pricing data your backend returns (adjust the fields to fit your actual response):
// Define a model to represent individual pricing entries from the backend struct PricingModel: Codable { let price: Double let currency: String let isPromotional: Bool? // Add any other fields your backend provides (e.g., salePrice, tieredPricing) } // Update Item to carry data via associated values enum Item { case Brand(String) case Collection(String?) // Optional if this field might be missing from responses case Dimensions(String) case SoldBy(String) case Category(String) case Pricing([PricingModel]) // Now holds an array of pricing data }
Step 2: Keep ProductSectionType and Refine ProductSection (if needed)
Your ProductSectionType enum still works perfectly for grouping sections. The ProductSection struct stays mostly the same, but now its items array will contain enum cases with attached data:
enum ProductSectionType { case ProductDetails case ProductPricing } struct ProductSection { var type: ProductSectionType var items: [Item] }
Step 3: Map Backend Product Data to Your Sections
Finally, we'll create a way to convert the backend's Product object into your ProductSection model. Here's an example of how to do that (adjust the field names to match your actual backend response):
// Assume this is the structure of the Product data your backend returns struct BackendProduct: Codable { let productCode: String let productBrand: String let productCategory: String let productDimensions: String let productSoldBy: String? let productCollection: String? let productPricings: [PricingModel] // The array of pricing data } // Convert backend product to your section-based model func convertToProductSections(from product: BackendProduct) -> [ProductSection] { // Build the product details section with all non-pricing items let detailsSection = ProductSection( type: .ProductDetails, items: [ .Brand(product.productBrand), .Collection(product.productCollection), .Dimensions(product.productDimensions), .SoldBy(product.productSoldBy ?? "Unknown"), .Category(product.productCategory) ] ) // Build the pricing section with the array of pricing data let pricingSection = ProductSection( type: .ProductPricing, items: [.Pricing(product.productPricings)] ) return [detailsSection, pricingSection] }
Key Notes
- Associated Values: This is the standard Swift approach to attach dynamic data to enum cases, which directly solves your problem of
Pricingneeding to hold an array. - PricingModel Customization: Make sure to tweak the
PricingModelproperties to exactly match the fields your backend sends for each pricing entry (e.g., maybeoriginalPrice,saleEndDate, etc.). - Optional Handling: Use optional associated values (like
Collection(String?)) if some fields might be missing from backend responses to avoid runtime crashes.
内容的提问来源于stack exchange,提问作者David Henry

