ios - 如何使用分钟数据过滤特定的一天时间作为 Swift 3 中多个索引中的单个索引存储到数组中

标签 ios swift xcode filter nsarray

我有两个数组。在该数组中,一个数组存储一些数字,如 1、3、4 等。

totalFilterActionArray : ["1", "21", "1", "2", "1", "2", "1", "1", "1", "20", "1", "1", "1", "1", "6", "1", "9", "1", "13"]

另一个是存储时间戳,就像每个小时都有一些时间戳一样

totalFilterActionTimeArray : ["11:22:34", "11:36:05", "11:38:00", "11:38:17", "11:38:31", "11:38:46", "11:40:51", "11:40:56", "11:41:25", "12:02:46", "12:12:59", "12:13:06", "12:58:46", "12:58:48", "12:59:39", "12:59:48", "01:00:02", "01:00:07", "01:00:21"]

所以,上面两个数组的索引计数是相同的。

现在,我想对于totalFilterActionArray数据的每个特定小时,就像上面假设的totalFilterActionTimeArray 11点钟有像“11:22:34”、“11:36:05”这样的间隔,我想把这些全部取11来自totalFilterActionArray 的时钟数据平均值(总和除以索引数)。然后下一个小时 12 点,假设一直持续到 24 小时。

如果假设该时间数组中缺少任何小时(例如 2 点钟或 4 点钟),我想将该小时数据添加为 0(零值)。

Here I want out put array is like newAppactionsarray is ["total 11'o clock appactions data sum/total 11'o clock time intervals count,"total 12'o clock appactions data sum/total 12'o clock time intervals count, till 24 hours];

我不需要时间,我只想要 appactionsarray 数据。

These data should store into other array, Because I am showing those data each hour as single line in some graph.

为此我尝试了以下操作

for(index , value) in totalFilterActionTimeArray.enumerated() {

 value.components(separatedBy: ":").first! 

   someotherArray.append(totalFilterActionArray[index])

   }

但是,上面的逻辑并没有像我们预期的那样工作,它获取所有小时的所有数据,我们如何过滤该数组数据中可用的每个小时,就像我在上面解释的那样。

我希望我在这里完整地解释了我的问题,如果有人遇到与此相关的问题,请在下面发表评论,我可以解释更多。

任何人都可以建议我,我们怎样才能实现这一目标?谢谢!

最佳答案

我不完全理解你的问题,但我希望这个解决方案对你有帮助..

var totalFilterActionArray = ["1", "21", "1", "2", "1", "2", "1", "1", "1"]
var totalFilterActionTimeArray = ["11:22:34", "11:36:05", "11:38:00", "11:38:17", "11:38:31", "11:38:46", "11:40:51", "11:40:56", "11:41:25"]
var gettingArray = [String]()
override func viewDidLoad() {

    super.viewDidLoad()
    configureView()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func configureView() {
    var array = [String]()
    var arrayInt = [Int]()
    var filterAction = 0

    for i in 1...24 {
        for(index , value) in totalFilterActionTimeArray.enumerated() {
            let time = value.components(separatedBy: ":").first!
            if Int(time)! <= i &&  Int(time)! > (i - 1) {
                filterAction = filterAction + Int(totalFilterActionArray[index])!
                arrayInt.append(Int(totalFilterActionArray[index])!)
                array.append(value)
            }

        }
//            print(arrayInt)
//            print(array)
        if array.count == 0 {
            gettingArray.append(0)
        } else {
            gettingArray.append(gettingTotalTime(array, filterActionValue: filterAction))
        }
        arrayInt.removeAll()
        array.removeAll()
        filterAction = 0
    }
    print(gettingArray)
}

func gettingTotalTime(_ array: [String], filterActionValue: Int) -> Int 
 {
     return filterActionValue/array.count
 }

关于ios - 如何使用分钟数据过滤特定的一天时间作为 Swift 3 中多个索引中的单个索引存储到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47811537/

相关文章:

ios - 如果同一个文件中有 2 个 Appdelegate 类会发生什么

ios - UICollectionView 委托(delegate)方法的问题

ios - 使用自定义维度在初始化时布局单元格 subview

c++ - 链接错误 __stack_chk_fail(将库与 xcode 一起使用)

ios - 由于未编译着色器,Unity 应用程序在 iOS 上崩溃

ios - didSelectItemAtIndexPath 修改 UICollectionView 中的多个单元格

ios - 如何在 SWIFT 中从 SKSpriteNode 创建 CGPath

iOS Animate 高度限制问题

ios - 如何使用自定义转换在当前上下文中呈现 View Controller ?

ios - 模拟器无法打开