数组中的Mutiline字符串

时间:2019-03-18 07:11:47

标签: ios swift3

我必须创建一个字符串formate来创建票据。我有一个包含字典的数组。我尝试但无法创建这样的字符串。 数组的结构是这样的。

[["orderid": 139, "productfullqty": 1, "productprice": 8.0, "productname": Falafel, "id": 544, "producthalfqty": , "productonfourthqty": , "productthirdqty": ], ["orderid": 139, "productfullqty":  12, "productprice":  5.4, "productname":  Tomato, "id": 545, "producthalfqty": , "productonfourthqty": , "productthirdqty": ], ["orderid": 139, "productfullqty":  18, "productprice":  180.0, "productname":  Green Sauce, "id": 546, "producthalfqty": , "productonfourthqty": , "productthirdqty": ], ["orderid": 139, "productfullqty":  1, "productprice":  0.46, "productname":  Onions, "id": 547, "producthalfqty": , "productonfourthqty": , "productthirdqty": ], ["orderid": 139, "productfullqty":  1, "productprice":  8.0, "productname":  Falafel, "id": 548, "producthalfqty": , "productonfourthqty": , "productthirdqty": ], ["orderid": 139, "productfullqty":  12, "productprice":  5.4, "productname":  Tomato, "id": 549, "producthalfqty": , "productonfourthqty": , "productthirdqty": ], ["orderid": 139, "productfullqty":  18, "productprice":  180.0, "productname":  Green Sauce, "id": 550, "producthalfqty": , "productonfourthqty": , "productthirdqty": ], ["orderid": 139, "productfullqty":  1, "productprice":  0.46, "productname":  Onions, "id": 551, "producthalfqty": , "productonfourthqty": , "productthirdqty": ]]

使用此数组,我必须创建这样的字符串。 enter image description here

谢谢。

3 个答案:

答案 0 :(得分:2)

您可以尝试这个。

let Falafel = "Falafel"
let Tomato = "Tomato"
let GreenSauce = "Green Sauce"
let Onions = "Onions"

let orders = [["orderid": 139, "productfullqty": 1, "productprice": 8.0, "productname": Falafel, "id": 544, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty": 1], ["orderid": 139, "productfullqty":  12, "productprice":  5.4, "productname":  Tomato, "id": 545, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty": 1], ["orderid": 139, "productfullqty":  18, "productprice":  180.0, "productname":  GreenSauce, "id": 546, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty": 1], ["orderid": 139, "productfullqty":  1, "productprice":  0.46, "productname":  Onions, "id": 547, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty": 1], ["orderid": 139, "productfullqty":  1, "productprice":  8.0, "productname":  Falafel, "id": 548, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty":1 ], ["orderid": 139, "productfullqty":  12, "productprice":  5.4, "productname":  Tomato, "id": 549, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty": 1], ["orderid": 139, "productfullqty":  18, "productprice":  180.0, "productname":  GreenSauce, "id": 550, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty": 1], ["orderid": 139, "productfullqty":  1, "productprice":  0.46, "productname":  Onions, "id": 551, "producthalfqty": 1, "productonfourthqty": 1, "productthirdqty": 1]]

var result = "SKU            Description                        Total\n"
var subTotal: Decimal = 0
orders.forEach { (order) in
    if let sku = order["id"],
        let name = order["productname"],
        let value = order["productprice"],
        let price = value as? NSNumber
    {
        let SKU = "\(sku)".cString(using: .utf8)!
        let NAME = "\(name)".cString(using: .utf8)!
        let PRICE = String(format:"%0.2f", price.doubleValue).cString(using: .utf8)!
        let string = String(format: "%-15s%-32s%8s\n", OpaquePointer(SKU), OpaquePointer(NAME), OpaquePointer(PRICE))
        result.append(string)
        subTotal += price.decimalValue
    }
}
result.append("Subtotal                                         \(subTotal)\n")
result.append("Tax                                                 \(0.00)\n")

print("\(result)")

答案 1 :(得分:0)

struct MyOrder {
let id: Int
let orderId: Int
let fullQuantity: Int
let halfQuantity: Int
let quarterQuantity: Int
let price: Double
let name: String

init?(dict: [String:Any]) {
    guard let id = dict["id"] as? Int,
        let name = dict["productname"] as? String
    //..
    //..
    // so on
    else { return nil }
    self.id = id
    self.name = name
}
}

let dataArray = [["id":0,"productname":"Oreo"]]
    let orders = dataArray.compactMap { MyOrder(dict: $0) }
    let myStringRows = orders.map{ "\($0.id)\t\t\($0.name)\t\t" } // format your data here.
    let myFinalTable = myStringRows.joined(separator: "\n")
    print(myFinalTable)

答案 2 :(得分:0)

首先,确保您的JSON为valid

第二,您可以使用以下方法将JSON解码为字典数组:

let order = try? JSONDecoder().decode([[String:Any]], from: #JSONString)