【问题标题】:Multiple Structs Arrays into a single struct type多个结构数组到一个结构类型
【发布时间】:2020-08-11 16:54:00
【问题描述】:

我希望从多个来源(Web REST 调用)检索两个数组数据结构数组,并将它们转换为一个可以在 UITableView 上使用的单一结构数组。

我最初走的是创建多维数组的道路,但当我不得不将类型向下转换为 [AnyObject] 并且无法读取属性时,这证明很困难。

示例数据:-

import UIKit

// First Set Of Data

struct Items : Codable{
    // List of 'items'
    let items : [Item]


}

struct Item: Codable{
    // Items inside the array

    let id : String
    let name : String
    let price : String
    let imageURL : String
    let weight : String

}

// ******** SECOND SET OF DATA

struct SecondSetOfItems : Codable {
    let mainFopCollection : MainFopCollection?

}

struct MainFopCollection : Codable {
    let sections : [Sections]?


}

struct Sections : Codable {
    let id : String?
    let fops : [Fops]?

}


struct Fops : Codable {
    let sku : String?
    let hash : Int?
    let product : Product?

}

struct Product : Codable {
    let sku : String?
    let name : String?
    let price : Price?

}

struct Price : Codable {
    let current : Double?
    let lpp : Bool?
    let unit : Unit?
    let type : String?


}

struct finalSetOfItems{

    let name : String?
    let price: String?


}

var array1 = [Items]()
var array2 = [SecondSetOfItems]()


// Originally tried:-

var multiArray = [[AnyObject]]()

multiArray.append(array1 as [AnyObject])
multiArray.append(array2 as [AnyObject])

// This part errors because I can't access this property
let name = multiArray[1][1].name

// Also tried a ternary operator (this part is within cellforrowat in my UITableViewController but I want the data to be dynamic, and not hard coded.

let name = indexPath.section == 0 ? array1![indexPath.row].name : array2![0].sections![0].fops![indexPath.row].product!.name

但后来我无法访问数组的属性,因为名称太模糊,而且由于我使用多种数据类型,因此很难明确我要查找的内容。

第二组数据比第一组更复杂(也更深入)。

所需的输出:-

// Take complex array1 and array2 and change them into a type of finalSetOfItems
var array1 = [Items]()
var array2 = [SecondSetOfItems]()
var arrayOutput = [finalSetOfItems]()

有没有办法可以将两组数据转换为单一结构类型,以便更容易访问属性? 最终目标是将每个数组转换为 UITableView 中自己的部分,以表示数据的来源

抱歉,如果我的问题措辞不当,我对 Swift 还是比较陌生。任何帮助将不胜感激

【问题讨论】:

    标签: arrays swift struct


    【解决方案1】:
    let firstItemSet = array1.flatMap{
        $0.items
    }.map{
        FinalSetOfItems(name: $0.name, price: $0.price)
    }
    
    let secondItemSet = array2.flatMap{
        $0.mainFopCollection?.sections ?? []
    }.flatMap{
        $0.fops ?? []
    }.compactMap{
        $0.product
    }.map{
        FinalSetOfItems(name: $0.name, price: $0.price)
    }
    
    let finalSet = firstItemSet + secondItemSet
    

    【讨论】:

    • 嗨,我在原帖中添加了更多上下文。本质上是想采用array1和array2,并使它们适合[finalSetOfItems]的更苗条的结构
    • @CodeTester9001 我进行了编辑。是你想要的吗?
    • 明天试试这个然后回来找你(刚要睡觉)。不过看起来很有希望!谢谢
    • 如果 Price 变成了它自己的结构,所以我在当前水平上获得了名称,但随后需要为价格再降低一个水平,我将如何去做呢?我已经编辑了第一个人以显示价格结构?提前致谢
    • @CodeTester9001 您的 struct Item 是否包含 price 作为 Price 类型而不是 String ?还要注意 FinalSetOfItems 应该将价格作为价格类型。编译器只是抱怨你使用错误的类型来初始化 FinalSetOfItems
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2023-03-06
    相关资源
    最近更新 更多