Append Data Type to Plist










0















I am parsing data and instansiating struct each Time root element (objstation) is parsed and encoding to DATA to write to plist



XML Structure



<objStation>
<StationDesc>Killester</StationDesc>
<StationAlias/>
<StationLatitude>53.373</StationLatitude>
<StationLongitude>-6.20442</StationLongitude>
<StationCode>KLSTR</StationCode>
<StationId>101</StationId>
</objStation>
<objStation>
<StationDesc>Clontarf Road</StationDesc>
<StationAlias/>
<StationLatitude>53.3629</StationLatitude>
<StationLongitude>-6.22753</StationLongitude>
<StationCode>CTARF</StationCode>
<StationId>109</StationId>
</objStation>


I am using the following method in my struct to encode and write to plist, however only the current instance of the struct is saved to the plist, I am wondering how to append to the plist, so all root elements are saved to the plist, I have played around with various swift native functions around the DATA type but have had no joy.
Maybe I should be using an array of dictionaries rather than a struct for this?



 //mark properties, update as necssary, based on xml strucutre
struct StationsEncoder: Codable

var station: String
var latitude: String
var longitude: String
var code: String
var id: String

//MARK: Initialization

init(station: String, latitude: String, longitude: String, code: String, id: String )
self.station = station
self.latitude = latitude
self.longitude = longitude
self.code = code
self.id = id


//mark initializers
// lets try encode our data
func encoder()
let stationStruct = StationsEncoder(station: station, latitude: latitude, longitude: longitude, code: code, id: id)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do
let data = try encoder.encode(stationStruct)
try data.write(to: CreatePlist.shared.plistURL)

catch
// Handle error
print(error)
print( CreatePlist.shared.plistURL)














share|improve this question



















  • 1





    You can't append to a plist. You need to load the current plist data, append to that array or dictionary, then write out the updated array or dictionary overwriting the plist with the new version.

    – rmaddy
    Nov 14 '18 at 0:39











  • You might want to consider using a database instead of a plist depending on the amount of data and your app's needs.

    – rmaddy
    Nov 14 '18 at 0:40











  • Thanks Maddy for explaining I cannot append to plist. Thought I maybe able to append to the DATA object and then write to the plist. I tried some native class methods provided for DATA type, but they did not seem to add to the data incrementally. I suppose I got carried away with trying to use a codable struct, but maybe they are just better for instances rather than trying to iterate through a full data set. I have only 20 xml root elements in this data (re using database). So it sounds like I should get the data into an array of dictionaries and then write entire lot to plist?

    – Frank Ferdinand McGovern
    Nov 14 '18 at 9:14















0















I am parsing data and instansiating struct each Time root element (objstation) is parsed and encoding to DATA to write to plist



XML Structure



<objStation>
<StationDesc>Killester</StationDesc>
<StationAlias/>
<StationLatitude>53.373</StationLatitude>
<StationLongitude>-6.20442</StationLongitude>
<StationCode>KLSTR</StationCode>
<StationId>101</StationId>
</objStation>
<objStation>
<StationDesc>Clontarf Road</StationDesc>
<StationAlias/>
<StationLatitude>53.3629</StationLatitude>
<StationLongitude>-6.22753</StationLongitude>
<StationCode>CTARF</StationCode>
<StationId>109</StationId>
</objStation>


I am using the following method in my struct to encode and write to plist, however only the current instance of the struct is saved to the plist, I am wondering how to append to the plist, so all root elements are saved to the plist, I have played around with various swift native functions around the DATA type but have had no joy.
Maybe I should be using an array of dictionaries rather than a struct for this?



 //mark properties, update as necssary, based on xml strucutre
struct StationsEncoder: Codable

var station: String
var latitude: String
var longitude: String
var code: String
var id: String

//MARK: Initialization

init(station: String, latitude: String, longitude: String, code: String, id: String )
self.station = station
self.latitude = latitude
self.longitude = longitude
self.code = code
self.id = id


//mark initializers
// lets try encode our data
func encoder()
let stationStruct = StationsEncoder(station: station, latitude: latitude, longitude: longitude, code: code, id: id)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do
let data = try encoder.encode(stationStruct)
try data.write(to: CreatePlist.shared.plistURL)

catch
// Handle error
print(error)
print( CreatePlist.shared.plistURL)














share|improve this question



















  • 1





    You can't append to a plist. You need to load the current plist data, append to that array or dictionary, then write out the updated array or dictionary overwriting the plist with the new version.

    – rmaddy
    Nov 14 '18 at 0:39











  • You might want to consider using a database instead of a plist depending on the amount of data and your app's needs.

    – rmaddy
    Nov 14 '18 at 0:40











  • Thanks Maddy for explaining I cannot append to plist. Thought I maybe able to append to the DATA object and then write to the plist. I tried some native class methods provided for DATA type, but they did not seem to add to the data incrementally. I suppose I got carried away with trying to use a codable struct, but maybe they are just better for instances rather than trying to iterate through a full data set. I have only 20 xml root elements in this data (re using database). So it sounds like I should get the data into an array of dictionaries and then write entire lot to plist?

    – Frank Ferdinand McGovern
    Nov 14 '18 at 9:14













0












0








0








I am parsing data and instansiating struct each Time root element (objstation) is parsed and encoding to DATA to write to plist



XML Structure



<objStation>
<StationDesc>Killester</StationDesc>
<StationAlias/>
<StationLatitude>53.373</StationLatitude>
<StationLongitude>-6.20442</StationLongitude>
<StationCode>KLSTR</StationCode>
<StationId>101</StationId>
</objStation>
<objStation>
<StationDesc>Clontarf Road</StationDesc>
<StationAlias/>
<StationLatitude>53.3629</StationLatitude>
<StationLongitude>-6.22753</StationLongitude>
<StationCode>CTARF</StationCode>
<StationId>109</StationId>
</objStation>


I am using the following method in my struct to encode and write to plist, however only the current instance of the struct is saved to the plist, I am wondering how to append to the plist, so all root elements are saved to the plist, I have played around with various swift native functions around the DATA type but have had no joy.
Maybe I should be using an array of dictionaries rather than a struct for this?



 //mark properties, update as necssary, based on xml strucutre
struct StationsEncoder: Codable

var station: String
var latitude: String
var longitude: String
var code: String
var id: String

//MARK: Initialization

init(station: String, latitude: String, longitude: String, code: String, id: String )
self.station = station
self.latitude = latitude
self.longitude = longitude
self.code = code
self.id = id


//mark initializers
// lets try encode our data
func encoder()
let stationStruct = StationsEncoder(station: station, latitude: latitude, longitude: longitude, code: code, id: id)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do
let data = try encoder.encode(stationStruct)
try data.write(to: CreatePlist.shared.plistURL)

catch
// Handle error
print(error)
print( CreatePlist.shared.plistURL)














share|improve this question
















I am parsing data and instansiating struct each Time root element (objstation) is parsed and encoding to DATA to write to plist



XML Structure



<objStation>
<StationDesc>Killester</StationDesc>
<StationAlias/>
<StationLatitude>53.373</StationLatitude>
<StationLongitude>-6.20442</StationLongitude>
<StationCode>KLSTR</StationCode>
<StationId>101</StationId>
</objStation>
<objStation>
<StationDesc>Clontarf Road</StationDesc>
<StationAlias/>
<StationLatitude>53.3629</StationLatitude>
<StationLongitude>-6.22753</StationLongitude>
<StationCode>CTARF</StationCode>
<StationId>109</StationId>
</objStation>


I am using the following method in my struct to encode and write to plist, however only the current instance of the struct is saved to the plist, I am wondering how to append to the plist, so all root elements are saved to the plist, I have played around with various swift native functions around the DATA type but have had no joy.
Maybe I should be using an array of dictionaries rather than a struct for this?



 //mark properties, update as necssary, based on xml strucutre
struct StationsEncoder: Codable

var station: String
var latitude: String
var longitude: String
var code: String
var id: String

//MARK: Initialization

init(station: String, latitude: String, longitude: String, code: String, id: String )
self.station = station
self.latitude = latitude
self.longitude = longitude
self.code = code
self.id = id


//mark initializers
// lets try encode our data
func encoder()
let stationStruct = StationsEncoder(station: station, latitude: latitude, longitude: longitude, code: code, id: id)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do
let data = try encoder.encode(stationStruct)
try data.write(to: CreatePlist.shared.plistURL)

catch
// Handle error
print(error)
print( CreatePlist.shared.plistURL)











ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 6:59







Frank Ferdinand McGovern

















asked Nov 13 '18 at 22:45









Frank Ferdinand McGovernFrank Ferdinand McGovern

155




155







  • 1





    You can't append to a plist. You need to load the current plist data, append to that array or dictionary, then write out the updated array or dictionary overwriting the plist with the new version.

    – rmaddy
    Nov 14 '18 at 0:39











  • You might want to consider using a database instead of a plist depending on the amount of data and your app's needs.

    – rmaddy
    Nov 14 '18 at 0:40











  • Thanks Maddy for explaining I cannot append to plist. Thought I maybe able to append to the DATA object and then write to the plist. I tried some native class methods provided for DATA type, but they did not seem to add to the data incrementally. I suppose I got carried away with trying to use a codable struct, but maybe they are just better for instances rather than trying to iterate through a full data set. I have only 20 xml root elements in this data (re using database). So it sounds like I should get the data into an array of dictionaries and then write entire lot to plist?

    – Frank Ferdinand McGovern
    Nov 14 '18 at 9:14












  • 1





    You can't append to a plist. You need to load the current plist data, append to that array or dictionary, then write out the updated array or dictionary overwriting the plist with the new version.

    – rmaddy
    Nov 14 '18 at 0:39











  • You might want to consider using a database instead of a plist depending on the amount of data and your app's needs.

    – rmaddy
    Nov 14 '18 at 0:40











  • Thanks Maddy for explaining I cannot append to plist. Thought I maybe able to append to the DATA object and then write to the plist. I tried some native class methods provided for DATA type, but they did not seem to add to the data incrementally. I suppose I got carried away with trying to use a codable struct, but maybe they are just better for instances rather than trying to iterate through a full data set. I have only 20 xml root elements in this data (re using database). So it sounds like I should get the data into an array of dictionaries and then write entire lot to plist?

    – Frank Ferdinand McGovern
    Nov 14 '18 at 9:14







1




1





You can't append to a plist. You need to load the current plist data, append to that array or dictionary, then write out the updated array or dictionary overwriting the plist with the new version.

– rmaddy
Nov 14 '18 at 0:39





You can't append to a plist. You need to load the current plist data, append to that array or dictionary, then write out the updated array or dictionary overwriting the plist with the new version.

– rmaddy
Nov 14 '18 at 0:39













You might want to consider using a database instead of a plist depending on the amount of data and your app's needs.

– rmaddy
Nov 14 '18 at 0:40





You might want to consider using a database instead of a plist depending on the amount of data and your app's needs.

– rmaddy
Nov 14 '18 at 0:40













Thanks Maddy for explaining I cannot append to plist. Thought I maybe able to append to the DATA object and then write to the plist. I tried some native class methods provided for DATA type, but they did not seem to add to the data incrementally. I suppose I got carried away with trying to use a codable struct, but maybe they are just better for instances rather than trying to iterate through a full data set. I have only 20 xml root elements in this data (re using database). So it sounds like I should get the data into an array of dictionaries and then write entire lot to plist?

– Frank Ferdinand McGovern
Nov 14 '18 at 9:14





Thanks Maddy for explaining I cannot append to plist. Thought I maybe able to append to the DATA object and then write to the plist. I tried some native class methods provided for DATA type, but they did not seem to add to the data incrementally. I suppose I got carried away with trying to use a codable struct, but maybe they are just better for instances rather than trying to iterate through a full data set. I have only 20 xml root elements in this data (re using database). So it sounds like I should get the data into an array of dictionaries and then write entire lot to plist?

– Frank Ferdinand McGovern
Nov 14 '18 at 9:14












1 Answer
1






active

oldest

votes


















0














I accomplished this with a tiny bit of code using PropertyListSerialization method (thanks again to Maddy who saved time pointing out that you cannot append to a plist). The best option I found was to append to a dictionary first, then write out entire dictionary to plist:



do 
let writeData = try PropertyListSerialization.data(fromPropertyList: [parsedDictionary], format: .xml, options:0)
//note CreatePlist.shared.plistURL is my URL, just replace with yours, I am accessing a singleton class that has the URL here for convenience:
try writeData.write(to: CreatePlist.shared.plistURL)
catch
print(error)






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290650%2fappend-data-type-to-plist%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I accomplished this with a tiny bit of code using PropertyListSerialization method (thanks again to Maddy who saved time pointing out that you cannot append to a plist). The best option I found was to append to a dictionary first, then write out entire dictionary to plist:



    do 
    let writeData = try PropertyListSerialization.data(fromPropertyList: [parsedDictionary], format: .xml, options:0)
    //note CreatePlist.shared.plistURL is my URL, just replace with yours, I am accessing a singleton class that has the URL here for convenience:
    try writeData.write(to: CreatePlist.shared.plistURL)
    catch
    print(error)






    share|improve this answer





























      0














      I accomplished this with a tiny bit of code using PropertyListSerialization method (thanks again to Maddy who saved time pointing out that you cannot append to a plist). The best option I found was to append to a dictionary first, then write out entire dictionary to plist:



      do 
      let writeData = try PropertyListSerialization.data(fromPropertyList: [parsedDictionary], format: .xml, options:0)
      //note CreatePlist.shared.plistURL is my URL, just replace with yours, I am accessing a singleton class that has the URL here for convenience:
      try writeData.write(to: CreatePlist.shared.plistURL)
      catch
      print(error)






      share|improve this answer



























        0












        0








        0







        I accomplished this with a tiny bit of code using PropertyListSerialization method (thanks again to Maddy who saved time pointing out that you cannot append to a plist). The best option I found was to append to a dictionary first, then write out entire dictionary to plist:



        do 
        let writeData = try PropertyListSerialization.data(fromPropertyList: [parsedDictionary], format: .xml, options:0)
        //note CreatePlist.shared.plistURL is my URL, just replace with yours, I am accessing a singleton class that has the URL here for convenience:
        try writeData.write(to: CreatePlist.shared.plistURL)
        catch
        print(error)






        share|improve this answer















        I accomplished this with a tiny bit of code using PropertyListSerialization method (thanks again to Maddy who saved time pointing out that you cannot append to a plist). The best option I found was to append to a dictionary first, then write out entire dictionary to plist:



        do 
        let writeData = try PropertyListSerialization.data(fromPropertyList: [parsedDictionary], format: .xml, options:0)
        //note CreatePlist.shared.plistURL is my URL, just replace with yours, I am accessing a singleton class that has the URL here for convenience:
        try writeData.write(to: CreatePlist.shared.plistURL)
        catch
        print(error)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 17 '18 at 16:11









        rmaddy

        243k27320382




        243k27320382










        answered Nov 17 '18 at 6:56









        Frank Ferdinand McGovernFrank Ferdinand McGovern

        155




        155





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290650%2fappend-data-type-to-plist%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20