0

I have an array of strings that have been converted from a date into a String from Parse like this:

var createdAt = object.createdAt
            if createdAt != nil {

            let date = NSDate()
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat =  "MM/dd/YYY/HH/mm/ss"
            let string = dateFormatter.stringFromDate(date)
            let arrayOfCompontents = string.componentsSeparatedByString("/")

            let dateTimeString = dateFormatter.stringFromDate(createdAt as! NSDate!)

                self.timeCreatedString.append("\(arrayOfCompontents[0...2])")

I'm appending to an Array called timeCreatedString.

When I print to the logs the output is: ["[\"10\", \"26\", \"2015\"]"]

And when I put it on a UILabel I get this: ["10", "26", "2015"]

Is there a simple way to remove the brackets, quotes and commas from a swift array and replace it with something else (or nothing)?

2 Answers 2

1

When you are using

self.timeCreatedString.append("\(arrayOfCompontents[0...2])")

it actually creates a new array with given range, then gets its "description" and adds that description to the array, you want to append actual items and not the description, the one way to do it is

self.timeCreatedString += arrayOfCompontents[0...2]

or

self.timeCreatedString.appendContentsOf(arrayOfCompontents[0...2])

if you want whole date string to be appended at once, then use

self.timeCreatedString.append("\(arrayOfCompontents[0]) \(arrayOfCompontents[1]) \(arrayOfCompontents[2])")
Sign up to request clarification or add additional context in comments.

1 Comment

I'm add each date to a UILabel in a cell in my TableViewController. At the moment, it's printing each component separately: the month in 1 cell, the year in another cell, etc.
0

You could create an extension which shows a string of all the array elements:

extension CollectionType where Generator.Element == String {
    var prettyPrinted: String {
        return self.joinWithSeparator(" ")
    }
}

Example usage:

let arr = ["10", "26", "2015"]

let pretty = arr.prettyPrinted

print(pretty)  // "10 26 2015"

1 Comment

How could i take out other elements within the array? For example, I wanted to joinWithSeparator(" ") and I wanted to take out all characters == "1"?

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.