0

I have to create a string formate for creating bills.I have an array contains dictionaries.I tried but not able to create string like this. Structure of array is like this.

[["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": ]]

Using this array, I have to create string like this. enter image description here

Thanks in advance.

6
  • 1
    Why not convert this collection to array of objects? It would be cleaner to operate. Once you done that append a new line with your data and spacing format value through a for loop or map function and later with that string array you can also use joined() function for collections by passing "\n" new line as a separator. Commented Mar 18, 2019 at 7:18
  • Can you please elaborate with example or URL thanks. Commented Mar 18, 2019 at 7:21
  • Do you want to have the same alignment? Because it will work only if the font used to print it later is monospace. What have you tried? Commented Mar 18, 2019 at 9:07
  • @Larme , Thanks for response , We need to print customer order bill via star micronics printer. And i got one demo from star micronics support , in that this type of demo bill format is given. So , we are considering this format to print bill. Commented Mar 18, 2019 at 10:05
  • 1
    As a first step, you need to get rid of the array of dictionaries and create a struct that represents each line item. This will dramatically simplify your code. Commented Mar 18, 2019 at 12:49

3 Answers 3

2

You could try this.

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)")
Sign up to request clarification or add additional context in comments.

1 Comment

Just fixed your data structure. Look at: " "producthalfqty": , "productonfourthqty": , "productthirdqty": ] " You should put some values or do not add these keys
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)

Comments

0

First, ensure that your JSON is valid

Second, you can decode your JSON as an array of dictionary with:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.