Home > OS >  How init using a plist values - Swift Xcode
How init using a plist values - Swift Xcode

Time:01-26

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.

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

CodePudding user response:

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
        )
    )
}
  •  Tags:  
  • Related