Search code examples
arraysswiftprintingparentheses

how to remove () from print (swift3)


My code below prints out what I want its just I want to remove these (). enter image description here The line that does the printing has a comment above it. It should be simple I just don't know what to do.

   import UIKit

    class ViewController: UIViewController {
    @IBOutlet var entry: UILabel!
             var numbersWithCreationDates = [(Int,String)]()
       @IBOutlet var label: UITextField!

    @IBAction func enterScore(_ sender: Any) {

    if let text = label.text {
        if let number = Int(text){
               // let numberAndDate = (number,Date())

            let date = Date().description
            let numberAndDate = (number, date.substring(to: date.characters.index(of: "+")!))
                numbersWithCreationDates.insert(numberAndDate, at: 0)
            //line in question
            entry.text = numbersWithCreationDates.map { "\($0)" }.joined(separator:"\n\n")

            }}

        else {
        entry.text = "Please Enter Number"

        }}}
extension Date {
    static var formattedNow: String {
        let now = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
        dateFormatter.timeZone = TimeZone(identifier: "EST")
        return dateFormatter.string(from: now)
    }}

Solution

  • Your output is the result of converting a tuple to a String.

    You can change:

    entry.text = numbersWithCreationDates.map { "\($0)" }.joined(separator:"\n\n")
    

    to:

    entry.text = numbersWithCreationDates.map { "\($0.0), \($0.1)" }.joined(separator:"\n\n")
    

    This puts each value of the tuple in the string instead of the tuple itself. This also gets rid of the quotes around the date string. Of course you can format that output any way you wish.