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 init using a plist values – Swift Xcode

I need to do this Replace, but I don’t know how starts

"Replace the default AssessmentConfiguration init for a init using the plist values"

I need to use the propertyList(from:options:format:), but I don’t understand how to do that.

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

var configuration: AssessmentConfiguration = AssessmentConfiguration(
    outputFile: URL(fileURLWithPath: "/tmp/test_assessment_output.txt"),
    errorFile: URL(fileURLWithPath: "/tmp/test_assessment_error.txt"),
    runConfiguration: AssessmentConfiguration.RunConfiguration(runInterval: 30, iterations: 3)
) 

the plist file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>OutputFile</key>
    <string>/tmp/test_assessment_output.txt</string>
    <key>ErrorFile</key>
    <string>/tmp/test_assessment_error.txt</string>
    <key>RunConfiguration</key>
    <dict>
        <key>RunInterval</key>
        <integer>30</integer>
        <key>Iterations</key>
        <string>3</string>
    </dict>
</dict> 
</plist> 

after that, I need to Decode the .plist file and use its values to init the AssessmentConfiguration object

>Solution :

You can get your values from the info dictionary Bundle.main.infoDictionary and call that method with the values from the info plist. Something like:

var configuration: AssessmentConfiguration?
if let dictionary = Bundle.main.infoDictionary,
    let outputFile = dictionary["OutputFile"] as? String,
    let errorFile = dictionary["ErrorFile"] as? String,
    let runConfiguration = dictionary["RunConfiguration"] as? [String Any],
    let runInterval = runConfiguration["RunInterval"] as? Int,
    let iterations = runConfiguration["Iterations"] as? String {
       configuration = AssessmentConfiguration(
            outputFile: URL(fileURLWithPath: outputFile),
            errorFile: URL(fileURLWithPath: errorFile),
            runConfiguration: AssessmentConfiguration.RunConfiguration(runInterval: runInterval, iterations: Int(iterations) ?? 0
        )
    )
}
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