ios - 检查多个数组的特定值并计算另一个值的平均值 - swift

标签 ios arrays swift

我有一些非常困惑的代码——至少我是这么认为的!我想问问你们,是否有更简单的方法来做到这一点,这样这部分代码就不会占用那么多空间。

这是我的代码能够做到的: 使用 components(separatedBy: "") 将一个字符串变成一个数组。它需要这样做,因为 allCars 实际上是一个正在上线的值。

检查最后一个值以获取车辆的类别。检查有多少个汽车类别,摩托车类别等。

检查车辆的重量(通过查看countingVehicleOnecountingVehicleTwo ...等中特定车辆的数组)

计算每辆车的平均值。

我应该说有时不会有汽车..有时不会有摩托车 - 这非常重要。

代码如下:

let allCars = "Peugeot#950#yellow#car--Yamaha#180#black#motorcycle--Kawazasaki#210#green#motorcycle"



let countingCategories = allCars.components(separatedBy: "--")

var carWeight: Int = 0
var motorcycleWeight: Int = 0
var carCount: Int = 0
var motorcycleCount: Int = 0



if countingCategories.count == 1 {

    let countingVehicleOne = countingCategories[0].components(separatedBy: "#")





    if countingVehicleOne[3] == "car" {
        carWeight = Int(countingVehicleOne[1])!
        var carCount: Int = 1

    } else if countingVehicleOne[3] == "motorcycle" {
        motorcycleWeight = Int(countingVehicleOne[1])!
        var motorcycleCount: Int = 1

    }

} else if countingCategories.count == 2 {

    let countingVehicleOne = countingCategories[0].components(separatedBy: "#")
    let countingVehicleTwo = countingCategories[1].components(separatedBy: "#")



    let numbers:[[String]] = [countingVehicleOne, countingVehicleTwo]


    for number in numbers {
        if number.last == "car"{
            carCount = carCount + 1
        }
    }


    for number in numbers {
        if number.last == "motorcycle"{
            motorcycleCount = motorcycleCount + 1
        }
    }



    if countingVehicleOne[3] == "car" {
        carWeight = Int(countingVehicleOne[1])!

    } else if countingVehicleOne[3] == "motorcycle" {
        motorcycleWeight = Int(countingVehicleOne[1])!

    }



    if countingVehicleTwo[3] == "car" {
        carWeight = Int(countingVehicleTwo[1])! + carWeight

    } else if countingVehicleTwo[3] == "motorcycle" {
        motorcycleWeight = Int(countingVehicleTwo[1])! + motorcycleWeight

    }




} else if countingCategories.count == 3 {

    let countingVehicleOne = countingCategories[0].components(separatedBy: "#")
    let countingVehicleTwo = countingCategories[1].components(separatedBy: "#")
    let countingVehicleThree = countingCategories[2].components(separatedBy: "#")



    let numbers:[[String]] = [countingVehicleOne, countingVehicleTwo, countingVehicleThree]


    for number in numbers {
        if number.last == "car"{
            carCount = carCount + 1
        }
    }


    for number in numbers {
        if number.last == "motorcycle"{
            motorcycleCount = motorcycleCount + 1
        }
    }



    if countingVehicleOne[3] == "car" {
        carWeight = Int(countingVehicleOne[1])!

    } else if countingVehicleOne[3] == "motorcycle" {
        motorcycleWeight = Int(countingVehicleOne[1])!

    }



    if countingVehicleTwo[3] == "car" {
        carWeight = Int(countingVehicleTwo[1])! + carWeight

    } else if countingVehicleTwo[3] == "motorcycle" {
        motorcycleWeight = Int(countingVehicleTwo[1])! + motorcycleWeight

    }

    if countingVehicleTwo[3] == "car" {
        carWeight = Int(countingVehicleThree[1])! + carWeight

    } else if countingVehicleTwo[3] == "motorcycle" {
        motorcycleWeight = Int(countingVehicleThree[1])! + motorcycleWeight

    }




}

let averageCarWeight = carWeight / carCount
let averageMotorcycleWeight = motorcycleWeight / motorcycleCount
print("Average weight for cars: \(averageCarWeight), average weight for motorcycle: \(averageMotorcycleWeight)")

我不确定它是否可以用更简单的方式完成,但我想与你们确认是否有 :-) 感谢您抽出时间!

编辑 enter image description here

最佳答案

您好,您实际上可以使用高阶函数来实现您的需求。

let allVehicles = "Peugeot#950#yellow#car--Yamaha#180#black#motorcycle--Kawazasaki#210#green#motorcycle"

let vehichlesArray = allVehicles.componentsSeparatedByString("--")
let vehicles = vehichlesArray.map { $0.componentsSeparatedByString("#") }

let cars = vehicles.filter { $0[3] == "car" }
let motorcycles = vehicles.filter { $0[3] == "motorcycle" }

let carsCount = cars.count
let motorcyclesCount = motorcycles.count

let carWgt = cars.reduce(0, combine: { $0 + (Int($1[1]) ?? 0) })
let motorcycleWgt = motorcycles.reduce(0, combine: { $0 + (Int($1[1]) ?? 0) })

不过,我相信面向对象的架构更适合这种场景。

关于ios - 检查多个数组的特定值并计算另一个值的平均值 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39891874/

相关文章:

swift - Charts3.0 - 如果超过 8 个值,则无法显示所有 xAxis 标签 - Swift

iphone - - (void)add<名称>对象 : function

const void 指针的 C++ 语法

c - scanf() 改变矩阵索引的值

swift - 根据禁用与否更改SwiftUI中按钮的颜色

ios - 将物理体添加到 Spritekit 节点

ios - SKLightNode 性能问题

ios - 我怎样才能在 iOS 5 中使用 CMDeviceMotion 获取设备的标题

iOS 8.3 - UITableView 单元格未对齐,向左缩进

java - 在 Groovy 注释中传递枚举数组