gpt4 book ai didi

ios - 来自数组的多行字符串

转载 作者:行者123 更新时间:2023-11-28 20:53:14 28 4
gpt4 key购买 nike

我必须创建一个字符串 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

提前致谢。

最佳答案

你可以试试这个。

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)")

关于ios - 来自数组的多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55216183/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com