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

时间:2016-10-06 09:03:53

标签: ios arrays swift

我有一些相当混乱的代码 - 至少我是这么认为的!我想问你们,如果有一种更简单的方法可以做到这一点,那么这部分代码就不会占用那么多空间。

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

检查最后一个值以获取车辆类别。检查有多少car个类别,motorcycle类别等。

检查车辆的重量(通过查看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

1 个答案:

答案 0 :(得分:1)

您好,您实际上可以使用更高阶的功能来实现您的需求。

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) })

但是,我相信面向对象的架构将更适合这种情况。