1

I downloaded a plist file from over the internet via a URLSession. As a result, I have the data (of type Data) of the plist file.

How can I get the bundle-version out of the file? Note that this does not work because the file is downloaded over the internet.

I already used Google to do some research, but I wasn't able to find anything about this. There was XMLParser, which could be used to parse xml files, but those files don't have a key and a value in a separated tag.

The plist looks like following:

<?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>items</key>
    <array>
        <dict>
            <key>assets</key>
            <array>
                <dict>
                    <key>kind</key>
                    <string>software-package</string>
                    <key>url</key>
                    <string>***</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>display-image</string>
                    <key>url</key>
                    <string>***</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>full-size-image</string>
                    <key>url</key>
                    <string>***</string>
                </dict>
            </array>
            <key>metadata</key>
            <dict>
                <key>bundle-identifier</key>
                <string>***</string>
                <key>bundle-version</key>
                <string>***</string>
                <key>kind</key>
                <string>software</string>
                <key>title</key>
                <string>***</string>
            </dict>
        </dict>
    </array>
</dict>
</plist>

How can I parse the bundle-version with Swift?

j3141592653589793238
  • 1,810
  • 2
  • 16
  • 38
  • Is this https://github.com/drmohundro/SWXMLHash help – yue you Feb 06 '18 at 15:53
  • @yueyou Well, I'd prefer to do this without external framworks – j3141592653589793238 Feb 06 '18 at 15:58
  • 1
    The XMLParser was where I thought to look first. However, Foundation has PropertyListSerialization.data(fromPropertyList: format: options:). That'll give you an Any, so then you probably need to cast it as an array, then cast metadata as a dictionary, then cast bundle-version as a String. – Barry Jones Feb 06 '18 at 16:09
  • @BarryJones You're right. This casting takes a long time... ```po (((dic["items"] as! NSArray).firstObject as! [String : Any])["metadata"] as! [String : Any])["bundle-version"] as! String``` printed the right version. Is there another opportunity without using ```NSArray.firstObject```? Because this seems pretty unclean to me. – j3141592653589793238 Feb 06 '18 at 16:12
  • 1
    I think you can use a regular Swift array, e.g. 'as [Any]!' or 'as [AnyObject']!', then use subscript syntax to get [0]. – Barry Jones Feb 06 '18 at 16:20
  • Alright, thanks, this worked well! Thanks for helping me out. – j3141592653589793238 Feb 06 '18 at 16:20

0 Answers0