xcode 如何使用 PlistBuddy 将数组添加到 Plist?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15573017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add an Array to a Plist using PlistBuddy?
提问by jhthorp
This question is a Sub-Question/Alternative Way to approach what I am trying to do via this question: How can I use PListBuddy to copy an entry from one file to another?
这个问题是一个子问题/替代方法,可以通过这个问题来解决我想要做的事情:如何使用 PListBuddy 将条目从一个文件复制到另一个文件?
I want to copy an Entry from Plist File A to Plist File B using PlistBuddy through an XCode Build Script, using Bash/Shell. By doing this, I cannot use PlistBuddy's "Copy" function. I must copy each individual entry over with all of it's elements, since you cannot add/set dictionaries, but must interact with PlistBuddy via "Entries".
我想使用 PlistBuddy 通过 XCode 构建脚本,使用 Bash/Shell 将条目从 Plist 文件 A 复制到 Plist 文件 B。通过这样做,我无法使用 PlistBuddy 的“复制”功能。我必须复制每个条目及其所有元素,因为您不能添加/设置字典,但必须通过“条目”与 PlistBuddy 交互。
What I am trying to do is take Plist File A and store its Titles/Values Arrays for a "PSMultiValueSpecifier" element. I am able to grab the elements in the arrays and print them to the screen, but when I go to add them into the destination Plist file B, the arrays are still empty.
我想要做的是获取 Plist 文件 A 并将其标题/值数组存储为“PSMultiValueSpecifier”元素。我能够获取数组中的元素并将它们打印到屏幕上,但是当我将它们添加到目标 Plist 文件 B 中时,数组仍然是空的。
Code: (Code to Grab the Entry for Adding)
代码:(获取添加条目的代码)
# Configure the Entry
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX dict" ${DEST_PLIST}
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Type string 'PSMultiValueSpecifier'" ${DEST_PLIST}
# Retrieve the Additional Field Value
preferenceTitle=`$PLISTBUDDY -c "Print PreferenceSpecifiers:$SOURCE_INDEX:Title" $SOURCE_PLIST 2>&1`
preferenceKey=`$PLISTBUDDY -c "Print PreferenceSpecifiers:$SOURCE_INDEX:Key" $SOURCE_PLIST 2>&1`
preferenceDefaultValue=`$PLISTBUDDY -c "Print PreferenceSpecifiers:$SOURCE_INDEX:DefaultValue" $SOURCE_PLIST 2>&1`
preferenceValues=`$PLISTBUDDY -c "Print PreferenceSpecifiers:$SOURCE_INDEX:Values" $SOURCE_PLIST 2>&1`
preferenceTitles=`$PLISTBUDDY -c "Print PreferenceSpecifiers:$SOURCE_INDEX:Titles" $SOURCE_PLIST 2>&1`
Code to Add the new Entry:
添加新条目的代码:
# Set the Additional Field Values
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Title string $preferenceTitle" ${DEST_PLIST}
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Key string $preferenceKey" ${DEST_PLIST}
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:DefaultValue integer $preferenceDefaultValue" ${DEST_PLIST}
####BORKEN####
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Values array $preferenceValues" ${DEST_PLIST}
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Titles array $preferenceTitles" ${DEST_PLIST}
####BORKEN####
Here is a snippet of Code that produces the below data:
这是产生以下数据的代码片段:
echo "#########"
echo "[$THIS] adding $preference: $preferenceDict"
echo "#########"
echo "Source: "`$PLISTBUDDY -c "Print PreferenceSpecifiers:$SOURCE_INDEX:Values" $SOURCE_PLIST`
echo "Source: "`$PLISTBUDDY -c "Print PreferenceSpecifiers:$SOURCE_INDEX:Titles" $SOURCE_PLIST`
echo "#########"
echo "Destination: "`$PLISTBUDDY -c "Print PreferenceSpecifiers:$DEST_INDEX:Values" $DEST_PLIST`
echo "Destination: "`$PLISTBUDDY -c "Print PreferenceSpecifiers:$DEST_INDEX:Titles" $DEST_PLIST`
echo "#########"
Here is the data provided proving that the proper fields are being transported
这是提供的数据,证明正在传输正确的字段
#########
[addDebugSettingsMenu.bash] adding : Dict {
Titles = Array {
Meters
Feet
}
DefaultValue = 1
Values = Array {
1
2
}
Key = UserPreferences_UnitsKey
Type = PSMultiValueSpecifier
Title = Units
}
#########
Source: Array { 1 2 }
Source: Array { Meters Feet }
#########
Destination: Array { }
Destination: Array { }
#########
Please help if you have any knowledge regarding undocumented features of PlistBuddy. The MAN pages are super slim and examples are far and between.
如果您对 PlistBuddy 的未记录功能有任何了解,请提供帮助。MAN 页面非常纤薄,示例非常多。
I would like to thank you for reading this and for lending your brains to help me solve this major pain in my neck.
我要感谢您阅读本文并感谢您的智慧帮助我解决颈部的这个主要疼痛。
采纳答案by jhthorp
There may be a better way to do this, but I have solved this problem by counting the elements in the source array and then copying them over individually.
可能有更好的方法来做到这一点,但我已经通过计算源数组中的元素然后单独复制它们来解决这个问题。
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Titles array" ${DEST_PLIST}
${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Titles:0 string 'TITLE_1'"
${DEST_PLIST} ${PLISTBUDDY} -c "Add PreferenceSpecifiers:$DEST_INDEX:Titles:1 string 'TITLE_2'" ${DEST_PLIST}
etc...
回答by nik
It may fix your problem but it is not the right way to implement in larger merging scripts. Check my ans below.
它可能会解决您的问题,但它不是在更大的合并脚本中实现的正确方法。在下面检查我的答案。
/usr/libexec/PlistBuddy -x -c "Print PreferenceSpecifiers" ${FROM_PLIST} > ${TO_PLIST}
回答by Ben Flynn
For something simple like an array of strings, here is code that will combine an arbitrary number of arrays. This example uses the UIAppFonts array common in iOS Info plists.
对于像字符串数组这样简单的东西,这里的代码将组合任意数量的数组。此示例使用 iOS 信息 plist 中常见的 UIAppFonts 数组。
TMP_NAME=`basename ##代码##`
TMP_DIR=`mktemp -d ${PROJECT_TEMP_DIR}/${TMP_NAME}.XXXXXX` || exit 1
FONTS_PLIST="${TMP_DIR}/Fonts.plist"
/usr/libexec/PlistBuddy -c "Add UIAppFonts array" "${FONTS_PLIST}"
# Iterate through each array, adding to the array entry in the temporary plist
index=0
for arg; do
status=0
inner_index=0
while [ ${status} -eq 0 ]; do
set +e
# No easy way to get array length, so keep going until we go out of bounds
entry=`/usr/libexec/PlistBuddy -c "Print UIAppFonts:${inner_index}" "${arg}" 2> /dev/null`
status=${?}
set -e
if [ ${status} -eq 0 ]; then
/usr/libexec/PlistBuddy -c "Add UIAppFonts:${index} string ${entry}" "${FONTS_PLIST}"
index=`expr ${index} + 1`
inner_index=`expr ${inner_index} + 1`
fi
done
done