ios - 我可以将 iOS Info.plist 转换为 ruby​​ 哈希吗?

标签 ios ruby hash plist

如果在另一个哈希中找到它们,我需要排除在我的应用程序的 Info.plist 中找到的特定键。目前我可以像这样访问 Info.plist 中的各个键

setting4Str = `/usr/libexec/PlistBuddy -c \"print :MySettingsDict:setting4" {settings_file}`

而且我可以在 plist 字典中的这个键处设置“setting 4 text”字符串...

但是,我希望能够遍历此 MysettingsDict 的所有键。有没有人有将 iOS XML plist 转换为 ruby​​ 字典的方法?

<?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>CFBundleDisplayName</key>
    <string>My App</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIdentifier</key>
    <string>com.company.appname</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleLocalizations</key>
    <array>
        <string>en</string>
        <string>de</string>
        <string>it</string>
        <string>fr</string>
        <string>ru</string>
        <string>es</string>
    </array>
    <key>CFBundleName</key>
    <string>${PRODUCT_NAME}</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.3.2</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.company.mySchemeName</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>mySchemeName</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>1.3.15.04170</string>
    <key>Internal version</key>
    <string>1.3.15.04170</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>MysettingsDict</key>
    <dict>
        <key>setting1</key>
        <false/>
        <key>setting2</key>
        <false/>
        <key>setting3</key>
        <false/>
        <key>setting4</key>
        <string>setting 4 text</string>
        <key>setting5</key>
        <string>setting 5 text</string>
        <key>setting6</key>
        <false/>
        <key>setting7</key>
        ...

最佳答案

为那些想要解析 binary Info.plist 的人添加。

info_plist = File.read("Info.plist") # read binary plist
IO.popen('plutil -convert xml1 -r -o - -- -', 'r+') {|f|
  f.write(info_plist)
  f.close_write
  info_plist = f.read # xml plist
}

现在 info_plist 是一个 xml 字符串。要快速提取一个属性,您可以使用正则表达式:

app_bundle = info_plist.scan(/<key>CFBundleIdentifier<\/key>\s+<string>(.+)<\/string>/).flatten.first

parse xml and convert to hash.

关于ios - 我可以将 iOS Info.plist 转换为 ruby​​ 哈希吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755612/

相关文章:

ios - TableView 滞后太多并占用太多内存 Swift

html - Ruby:清理 HTML,使用 Hpricot 还是仅使用正则表达式?

Java hashCode方法最大返回值

ios - 如何从第一个数组中查找第二个数组中的对象? swift iOS

javascript - 为什么我会收到无效值错误?

ios - 从 CoreData 中检索最后 N 个唯一名称的谓词。

ruby-on-rails - Rails 5.0.1,postgresql - 带别名的外键和包含失败的别名表

ruby-on-rails - Ruby/Rails - JSON.parse/JSON.decode 问题

Javascript HashTable 使用对象键

java - 如果采用线性探测,删除是否比单独链接的情况更便宜?