Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

swift - An array and list of list properties

I have an array of the objects. Every single object has another list. How to get the whole list of properties from the list? So as you can see there is a StatioItem array. Each stationItem owns a list of sensorItem objects. I want to get a list of id properties. How to do it?

[StationItem {
    id = 114;
    stationId = 114;
    cityName = Wroc?aw;
    addressStreet = ul. Bartnicza;
    sensors = List<SensorItem> <0x6000001bb840> (
        [0] SensorItem {
            id = 642;
            stationId = 642;
            param = ParamItem {
                paramName = dwutlenek azotu;
                paramFormula = NO2;
                paramCode = NO2;
                idParam = 6;
            };
        },
        [1] SensorItem {
            id = 644;
            stationId = 644;
            param = ParamItem {
                paramName = ozon;
                paramFormula = O3;
                paramCode = O3;
                idParam = 5;
            };
        }
    );
}, StationItem {
    id = 70;
    stationId = 70;
    cityName = O?awa;
    addressStreet = ul. ?o?nierzy AK 9;
    sensors = List<SensorItem> <0x6000001b7840> (
        [0] SensorItem {
            id = 397;
            stationId = 397;
            param = ParamItem {
                paramName = dwutlenek azotu;
                paramFormula = NO2;
                paramCode = NO2;
                idParam = 6;
            };
        },
        [1] SensorItem {
            id = 400;
            stationId = 400;
            param = ParamItem {
                paramName = py? zawieszony PM10;
                paramFormula = PM10;
                paramCode = PM10;
                idParam = 3;
            };
        },
        [2] SensorItem {
            id = 20214;
            stationId = 20214;
            param = ParamItem {
                paramName = ozon;
                paramFormula = O3;
                paramCode = O3;
                idParam = 5;
            };
        }
    );
}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What do we need to do: Iterate the array, for each station found, iterate over its sensor array. For each sensor found, keep only its id.

let ids = stations.map{ aStation in aStation.sensors.map{ $0.id } }.flatMap{ $0 }

With Sample:

struct StationItem {
    let id: Int
    let sensors: [SensorItem]
}

struct SensorItem {
    let id: Int
}

let stations: [StationItem] = [StationItem(id: 114, sensors: [SensorItem(id: 642),
                                                              SensorItem(id: 644)]),
                               StationItem(id: 70, sensors: [SensorItem(id: 397),
                                                             SensorItem(id: 400),
                                                             SensorItem(id: 2014)])]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...