Skip to content

Commit

Permalink
Use PropertyListSerialization
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed Mar 12, 2020
1 parent 2e7e80a commit 2face95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Example-Info.plist
Expand Up @@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>3</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
Expand Down
37 changes: 7 additions & 30 deletions Sources/SwiftExampleCommand/main.swift
Expand Up @@ -2,38 +2,15 @@ print("Hello, Swift Command!")

import Foundation

class MyXMLStructure: NSObject, XMLParserDelegate {
var depth: Int = 0;
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
depth += 1
print(String(repeating: "", count: depth), "START ", elementName)

}
func parser(_ parser: XMLParser, foundCharacters word: String) {
let trimmed = word.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed != "" {
print(String(repeating: " ", count: depth + 1), "=", trimmed)
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
print(String(repeating: "", count: depth), "END ", elementName)
depth -= 1
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print("[1001] PARSE ERROR:", parseError)
}
}
let instance = MyXMLStructure()

let fpath = CommandLine.arguments[1]
print("[0001] ARG PATH:", fpath)

let url = NSURL(fileURLWithPath: fpath) as URL
var xmlparser = XMLParser(contentsOf: url)!
xmlparser.delegate = instance
let contents = FileManager.default.contents(atPath: fpath)!

let plist = try PropertyListSerialization.propertyList(from: contents, options: .mutableContainersAndLeaves, format: nil) as! NSMutableDictionary

plist["CFBundleVersion"] = "3"

if xmlparser.parse() {
print("[0002] DEPTH:", instance.depth)
} else {
print("[1002] :", xmlparser.parserError)
if let data = try? PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: .max) {
try data.write(to: NSURL(fileURLWithPath: fpath) as URL)
}

0 comments on commit 2face95

Please sign in to comment.