简体   繁体   English

将String数组转换为Int Array Swift

[英]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). String值转换为数组中的Int值后,当我打印到日志时,得到的结果是: [0, 0, 0, 0] ["18:56:08", "18:56:28", "18:57:23", "18:58:01"] [0, 0, 0, 0] ,输出应为: ["18:56:08", "18:56:28", "18:57:23", "18:58:01"] (不含引号和:冒号)。

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. 我假设我没有在正确的时间转换值,或者我的方法放置错误,这就是为什么我得到0 0 0 0输出的原因。

Here is my ViewController code: 这是我的ViewController代码:

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. 当我在另一个ViewControllerviewDidLoad方法中尝试此操作而没有解析/查询发生时,我得到了正确的输出:一个转换为Ints的数组。 I'm assuming there's an issue of when and where i'm converting the Strings into Ints. 我假设存在将字符串转换为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? 我应该从Date转换为Int吗? If so, how do I do that? 如果是这样,我该怎么做? Am i doing something else wrong? 我在做别的事情吗? I'm awfully confused.... 我很困惑。

Any help is very much appreciated! 很感谢任何形式的帮助!

If you have an NSDate object anyway, you can create the date string with the date formatter 如果仍然有NSDate对象,则可以使用日期格式化程序创建日期字符串

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 : 或者,如果您想要小时,分钟和秒的整数值,请使用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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM