xcode – 如何使用PlistBuddy通过其属性访问PreferencesSpecified的元素?

目前我正在使用这个代码
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $productVersion" "Test/Settings.bundle/Root.plist"

在构建阶段的脚本部分将产品版本放在应用程序设置的只读字段中.该字段具有首选项数组的位置1(从0开始).

我问是否有可能使用更强大的1来访问该领域,因为在开发期间,我或其他开发人员可能会意外改变位置.

无论其位置如何,我可以访问指定它的标识符的元素吗?

为了更好地解释我的需要,我写下了一个例子.我需要像1.2.345这样的数组的第二个字符串的字符串节点,我需要从0.0.0变为1.2.345.是否可以访问dict节点,而不表示它是数组中的第二个?我要求类似于在PlistBuddy中使用的xpath表达式(如果有的话).

<?xml version="1.0" encoding="UTF-8"?>
<dict>
<key>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Title</key>
        <string>Application info</string>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
    </dict>
    <dict>
        <key>DefaultValue</key>
        <string>0.0.0</string>
        <key>Key</key>
        <string>version</string>
        <key>Title</key>
        <string>Version</string>
        <key>Type</key>
        <string>PSTitleValueSpecifier</string>
    </dict>
    <dict>
        <key>DefaultValue</key>
        <string>0</string>
        <key>Key</key>
        <string>build</string>
        <key>Title</key>
        <string>Build</string>
        <key>Type</key>
        <string>PSTitleValueSpecifier</string>
    </dict>
         ...

解决方法

#!/bin/tcsh
set productVersion="1.2.345"
set theFile="~/Desktop/PlistBuddy/Root.plist"
set cnt=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:" ${theFile} | grep "Dict"|wc -l`
# echo "the count is: $cnt."
set cnt=`expr "$cnt" '-' '1'`

foreach idx (`seq 0 $cnt`)
    # echo "the index is: $idx."
    set val=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:Title" ${theFile}`
    # echo "the value of PreferenceSpecifiers:${idx}:Title: is ${val}."

    if ( "$val" == "Version" ) then
        echo "the index of the entry whose 'Title' is 'Version' is $idx."
        # Now set it
        /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:${idx}:DefaultValue $productVersion" ${theFile}

        # just to be sure that it worked
        set ver=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:DefaultValue" ${theFile}`
        echo 'PreferenceSpecifiers:$idx:DefaultValue set to: ' $ver
    endif
end

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...