如何获取 Xcode 配置文件中的嵌套值?
How to get nested value in Xcode configuration files?
我正在尝试在我的 Xcode 配置文件中获取特定值。
因此,如果我在上面的示例中打印第 0 项,我知道部分代码是 print(Bundle.main.infoDictionary["Fonts provided by application"])
。
但是,如果我向其中添加 ["Item 0"]
,它就不起作用了。
我该如何解决这个问题?如果 "Fonts provided by application"
键的类型是 Dictionary
,我该怎么做?
Info.Plist 是字典。因此,要访问字体数组,您可以使用
Bundle.main.infoDictionary["Fonts provided by application"]
但是要访问您需要使用的每个元素
(Bundle.main.infoDictionary["Fonts provided by application"] as? Array)?.startIndex
- 您的密钥有误。是
"UIAppFonts"
,而不是 "Fonts provided by application"
。后者就是Xcode. 中的显示
Bundle.main.infoDictionary["UIAppFonts"]
的结果是一个数组,而不是另一个字典。因此,您可以像访问任何其他数组一样访问数组的每个元素。
示例:
if let fonts = Bundle.main.infoDictionary["UIAppFonts"] as? [String] {
// List all of the fonts
for font in fonts {
print(font)
}
// Get the first font
let font = fonts[0]
// or more safely
if let font = fonts.first {
}
}
我正在尝试在我的 Xcode 配置文件中获取特定值。
因此,如果我在上面的示例中打印第 0 项,我知道部分代码是 print(Bundle.main.infoDictionary["Fonts provided by application"])
。
但是,如果我向其中添加 ["Item 0"]
,它就不起作用了。
我该如何解决这个问题?如果 "Fonts provided by application"
键的类型是 Dictionary
,我该怎么做?
Info.Plist 是字典。因此,要访问字体数组,您可以使用
Bundle.main.infoDictionary["Fonts provided by application"]
但是要访问您需要使用的每个元素
(Bundle.main.infoDictionary["Fonts provided by application"] as? Array)?.startIndex
- 您的密钥有误。是
"UIAppFonts"
,而不是"Fonts provided by application"
。后者就是Xcode. 中的显示
Bundle.main.infoDictionary["UIAppFonts"]
的结果是一个数组,而不是另一个字典。因此,您可以像访问任何其他数组一样访问数组的每个元素。
示例:
if let fonts = Bundle.main.infoDictionary["UIAppFonts"] as? [String] {
// List all of the fonts
for font in fonts {
print(font)
}
// Get the first font
let font = fonts[0]
// or more safely
if let font = fonts.first {
}
}