0

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!

1 Answer 1

1

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

7 Comments

For the first example I get an error saying: Cannot convert value of type 'NSDate?!' to expected argument type 'NSDate It only works like this: let string = dateFormatter.stringFromDate(createdAt as NSDate!) 2nd example) What do I do with that code? Am I still using the var createdAt = object.createdAt if createdAt != nil { to query it? Where do I convert it into an Int and then set it to countDown? I didn't understand where and how I should format the code to get an Int in the array so that I can use it in my timer.
createdAt is obviously an NSDate object. The second example extracts the hour, minute and second information out of the date object as Int values. This is more effective than create a date string and convert it to the desired format.
I have the values printing as Ints, but now I'm unable to access them and countdown from the 2nd index (the seconds) which thus countsDown the minutes and then hours after reaching 0, rescpitvely. Here's the gist of the improved code: gist.github.com/rinyfo4/9456343799c07c168812 Any ideas of how I can make that work?
I might be easier to convert H:M:S to seconds (H * 3600) + (M * 60) + S then decrement the value and convert it back to H:M:S to be displayed. All this can also be accomplished with an NSDateFormatter and an appropriate offset to be used as base date
Where do I convert it?
|

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.