Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get element in array Swift IOS

How can i get an array of Id from dataInfo when name or title is empty

My code

struct Data {
    let id: String
    let name: String
    let tilte: String
}

var dataID: [String]

let dataInfo = [ Data(id: "1", name: "", tilte: ""), Data(id: 2, name: "K", tilte: "A"), Data(id: 3, name: "", tilte: ""), Data(id: 4, name: "4", tilte: "")]

my expectations

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dataID = [1, 3, 4]

>Solution :

First of all, because of your data Info not map with your sturct.

Your data should look like this

let dataInfo = [ Data(id: "1", name: "", tilte: ""), Data(id: "2", name: "K", tilte: "A"), Data(id: "3", name: "", tilte: ""), Data(id: "4", name: "4", tilte: "")]

And for your question, it can divide into 2 step: Make a dataInfo when name or title is empty and get their id

Code will be like this

var dataID: [String]

let dataInfo = [ Data(id: "1", name: "", tilte: ""), Data(id: "2", name: "K", tilte: "A"), Data(id: "3", name: "", tilte: ""), Data(id: "4", name: "4", tilte: "")]

let dataInfoFilterEmpty = dataInfo.filter{($0.name == "" || $0.tilte == "")}
dataID = dataInfoFilterEmpty.map{$0.id} // ["1", "3", "4"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading