MacRuby:如何阅读plist文件?

时间:2012-01-12 22:19:46

标签: plist macruby

我敢打赌,这非常简单易行,但无法在网上找到基本的例子。如何使用MacRuby从项目中的plist文件中读取数据?

解决方案:

原来有几种不同的方法可以做到,但我发现(目前为止)最干净的是使用MacRuby辅助方法load_plist(将plist文件的内容转换为哈希)。此外,如果您正在使用XCode,则需要获取相对于您的应用包的文件路径:

# there's an AppConfig.plist file in my app bundle

config_path = NSBundle.mainBundle.pathForResource('AppConfig', ofType: 'plist')
@config = load_plist File.read(config_path)

1 个答案:

答案 0 :(得分:1)

这个slide deck about MacRuby有一些用于访问plist文件的示例代码(幻灯片77-80),其中的要点是用NSDictionary.dictionaryWithContentsOfFile打开文件然后像Ruby Hash一样操作它,然后用writeToFile_atomically再次写入。 NSDictionary文档可能对您有用;你可以find it here