ios - how to use more efficiently SWXMLHash and how to set the class that will receive the data properly? -
i'm building project train schedule app, , i'm using api returns xml file,to manipulate data i'm using library called swxmlhash.
i need extract name of departure station destinations , departure times.
i created class store these data, need improve because i'm having trouble extracting destinations , departure times.
i able loop on xml file , names of departure stations.
here class :
class station { var name : string var destinations:[(dest:string, times:[int])] init(withname name: string, destinations:[(dest:string, times:[int])]){ self.name = name self.destinations = destinations } }
here code extract names of stations :
// code loops on xml file , retrieves name of departure stations // create empty array of station class var stationstest = [station]() // retrieve name of departure stations , add them , array of station elem in xml["root"]["station"].all{ var stations = elem["name"].element!.text! var stationplaceholder = station(withname: stations, destinations: [(dest:"",times:[1])]) stationstest.append(stationplaceholder) }
my problem how can destinations of each station appropriate times of departure
i suppose problem in way implemented class, need find better solution.
here sample of xml file i'm working with:
<?xml version="1.0" encoding="utf-8"?><root><uri><! [cdata[http://api.bart.gov/api/etd.aspx? cmd=etd&orig=all&ramdom=1454366707766]]></uri><date>02/01/2016</date> <time>02:44:52 pm pst</time> <station> <name>lake merritt</name> <abbr>lake</abbr> <etd> <destination>daly city</destination> <abbreviation>daly</abbreviation> <estimate> <minutes>3</minutes> </estimate> <estimate> <minutes>10</minutes> </estimate> <estimate> <minutes>17</minutes> </estimate> </etd> <etd> <destination>dublin/pleasanton</destination> <estimate> <minutes>7</minutes> </estimate> <estimate> <minutes>22</minutes> </estimate> <estimate> <minutes>37</minutes> </estimate> </etd> <etd> <destination>fremont</destination> <estimate> <minutes>4</minutes> </estimate> <estimate> <minutes>14</minutes> </estimate> <estimate> <minutes>19</minutes> </estimate> </etd> <etd> <destination>richmond</destination> <estimate> <minutes>5</minutes> </estimate> <estimate> <minutes>19</minutes> </estimate> <estimate> <minutes>34</minutes> </estimate> </etd> </station> <station> <name>fruitvale</name> <etd> <destination>daly city</destination> <estimate> <minutes>6</minutes> </estimate> <estimate> <minutes>12</minutes> </estimate> <estimate> <minutes>22</minutes> </estimate> </etd> <etd> <destination>dublin/pleasanton</destination> <estimate> <minutes>10</minutes> </estimate> <estimate> <minutes>25</minutes> </estimate> <estimate> <minutes>40</minutes> </estimate> </etd>
i used below code in swxmlhash playground , works on machine:
// need model etd element has destination , etd elements class etd { var destination: string = "" var estimates = [string]() init(destination: string, estimates: [string]) { self.destination = destination self.estimates = estimates } } // each station has collection of etds (per xml) class station { var name : string var etds = [etd]() init(withname name: string, etds: [xmlindexer]){ self.name = name etd in etds { self.etds.append(etd( destination: etd["destination"].element!.text!, estimates: etd["estimate"].all.map { $0["minutes"].element!.text! }) ) } } } var stationstest = [station]() elem in xml["root"]["station"] { var stations = elem["name"].element!.text! var stationplaceholder = station(withname: stations, etds: elem["etd"].all) stationstest.append(stationplaceholder) }
basically, looks missing abstraction etd
class - station doesn't contain list of destinations, list of etds. each etd
has both destination and estimates minute.
Comments
Post a Comment