Lukesivi
Lukesivi

Reputation: 2226

Converting String array into Int Array Swift

After converting the String values into Int values in an array, when I print to the logs, all I get is: [0, 0, 0, 0] when the output should be: ["18:56:08", "18:56:28", "18:57:23", "18:58:01"] (without the quotations and the : colon).

I'm converting the string array, directly after the values have been added to the string array. I'm assuming that I'm not converting the values at the right time, or that my methods are placed wrong and that's why I get the 0 0 0 0 output.

Here is my ViewController code:

class FeedTableViewController: UITableViewController {


var productName = [String]()
var productDescription = [String]()
var linksArray = [String]()
var timeCreatedString = [String]()
var minuteCreatedString = [String]()



var intArray = Array<Int>!()

override func viewDidLoad() {
    super.viewDidLoad()


    var query = PFQuery(className: "ProductInfo")

    query.findObjectsInBackgroundWithBlock ({ (objects, error) -> Void in

    if let objects = objects {

        self.productName.removeAll(keepCapacity: true)
        self.productDescription.removeAll(keepCapacity: true)
        self.linksArray.removeAll(keepCapacity: true)
        self.timeCreatedString.removeAll(keepCapacity: true)

        for object in objects {

            self.productName.append(object["pName"] as! String)

            self.productDescription.append(object["pDescription"] as! String)

            self.linksArray.append((object["pLink"] as? String)!)


// This is where I'm querying and converting the date: 



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(createdAt as NSDate!)

            var arrayOfCompontents = string.componentsSeparatedByString("/")


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

            self.minuteCreatedString.append("\(arrayOfCompontents[3]):\(arrayOfCompontents[4]):\(arrayOfCompontents[5])")

                self.intArray = self.minuteCreatedString.map { Int($0) ?? 0}

                print("INT ARRAY \(self.intArray)")

                print(self.minuteCreatedString.map { Int($0) ?? 0})

                print(self.minuteCreatedString)


            }

            self.tableView.reloadData()


            }


        }


    })


  }

When I tried this in another ViewController in the viewDidLoad method without Parse/queries happening, I get the correct output: a converted array of Ints. I'm assuming there's an issue of when and where i'm converting the Strings into Ints.

In what order/where should I convert the array of Strings into an array of Ints? Should I convert from Date to Int instead? If so, how do I do that? Am i doing something else wrong? I'm awfully confused....

Any help is very much appreciated!

Upvotes: 0

Views: 740

Answers (1)

vadian
vadian

Reputation: 285079

If you have an NSDate object anyway, you can create the date string with the date formatter

let createdAt = NSDate() // or give date object
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat =  "HH:mm:ss"
let string = dateFormatter.stringFromDate(createdAt) // "18:56:08"

Or if you want the integer values of hours, minutes and seconds, use NSDateComponents:

let comps = NSCalendar.currentCalendar().components([.Hour, .Minute, .Second], fromDate: createdAt)
let hour = comps.hour
let minute = comps.minute
let seconds = comps.second
let intArray = [hour, minute, second]

Upvotes: 1

Related Questions