美文网首页
How to use PlistBuddy

How to use PlistBuddy

作者: ColaBean | 来源:发表于2017-08-18 17:12 被阅读76次

PlistBuddy

一个plist文件操作工具。记录下方便以后使用,常用以下4个方法:

  • Add 添加
  • Delete 删除
  • Set 更新
  • Print 读取
  • ...

更多方法可 --help, 以上具体使用方法如下:

Add

  • 添加一个key:code value:404
/usr/libexec/PlistBuddy "Add :code integer 404" t1.plist

结果如下:

Dict {
    code = 404
}

  • 添加Array,先创建一个数组变量list, 然后往list中添加数据
/usr/libexec/PlistBuddy "Add :list Array" t1.plist
/usr/libexec/PlistBuddy "Add :list: string a1" t1.plist
# 根据索引插入值
/usr/libexec/PlistBuddy "Add :list:0 string a2" t1.plist

结果如下:

Dict {
    list = Array {
        a2
        a1
    }
}

  • 添加Dict
/usr/libexec/PlistBuddy "Add :result Dict" t1.plist
/usr/libexec/PlistBuddy "Add :result:name string Tom" t1.plist
/usr/libexec/PlistBuddy "Add :result:age integer 18" t1.plist
/usr/libexec/PlistBuddy "Add :result:list Array" t1.plist
/usr/libexec/PlistBuddy "Add :result:list: string aa" t1.plist

结果如下:

Dict {
    result = Dict {
        age = 18
        list = Array {
            aa
        }
        name = Tom
    }
}

Delete

  • 根据key删除 对应的value
/usr/libexec/PlistBuddy "Delete :code" t1.plist

Set

  • 根据key更新value
/usr/libexec/PlistBuddy "Set :code 200"

结果如下:

Dict {
    code = 200
}

Print

  • 根据key读取value
/usr/libexec/PlistBuddy "Print" t1.plist
/usr/libexec/PlistBuddy "Print :code" t1.plist

结果如下:

# t1.plist
Dict {
    result = Dict {
        age = 18
        list = Array {
            aa
        }
        name = Tom
    }
    code = 200
    list = Array {
        a2
        a1
    }
}
# code
200


Demo

相关文章

网友评论

      本文标题:How to use PlistBuddy

      本文链接:https://www.haomeiwen.com/subject/xfiarxtx.html