我如何 return 来自 plist 文件的特定值?
How do I return a specific value from a plist file?
所以我有一个名为 Computer 的 plist 文件,上面有 5 个项目。
每个项目都有这些键:
Type
Price
UniqueCode
例如
Item 0
Type: Desktop, Price 100, UniqueCode 321
Item 1
Type: Tablet, Price 150, UniqueCode 322
Item 2
Type: Laptop, Price 200, UniqueCode 323
Item 3
Type: Desktop, Price 210, UniqueCode 324
Item 4
Type: Laptop, Price 400, UniqueCode 325
然后我有一个class:
并且我输入了以下代码:
var emojiArray: NSArray?
if let path = NSBundle.mainBundle().pathForResource("Computer", ofType: "plist"){
emojiArray = NSArray(contentsOfFile: path)
}
我知道它不完整,但我只能做到这一点。我的主要objective是显示UniqueCode 321和324的电脑的价格,同时我也要把他们的价格减半
我该如何编码?
从你的 plist 描述来看,你似乎有一个字典数组。
看起来像这样的东西:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Type</key>
<string>Desktop</string>
<key>Price</key>
<integer>100</integer>
<key>Unique Code</key>
<integer>321</integer>
</dict>
<dict>
<key>Type</key>
<string>Laptop</string>
<key>Price</key>
<integer>400</integer>
<key>Unique Code</key>
<integer>235</integer>
</dict>
</array>
</plist>
因此,一旦加载了 emojiArray,就可以遍历数组并获取所需的值。
类似于:
if let path = Bundle.main.path(forResource: "Computer", ofType: "plist") {
if let emojiArray = NSArray(contentsOfFile: path) {
if let typedEmojiArray = emojiArray as? Array< Dictionary< String, AnyObject > > {
for eachEntry in typedEmojiArray {
if let uniqueCode = eachEntry["Unique Code"] as? NSNumber {
if let computerType = eachEntry["Type"] as? String {
print("computer type is \(computerType) and unique code is \(uniqueCode)")
}
if uniqueCode == 321 {
print("this is a value we wanted")
}
}
}
}
}
}
这是在 Swift 3、b.t.w 中完成的。
此外,像这样的 Plist 不一定是在文件中存储数据的最佳方式。如果这是我的应用程序,我可能会尝试使用 CoreData(在启动之间保留数据)。
首先,强烈建议您不要在 Swift 中使用 NSArray / NSDictionary
,因为 Foundation 集合类型缺少类型信息。反序列化 属性 列表的推荐 class 是 PropertyListSerialization
.
(Swift 3) 代码打印 UniqueCode
的值和 Price
的一半 UniqueCode
在 uniqueCodes
[= 中的所有项目18=]
let uniqueCodes = [321, 324]
if let url = Bundle.main.url(forResource:"Computer", withExtension: "plist") {
do {
let data = try Data(contentsOf:url)
let emojiArray = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [[String:Any]]
let filteredArray = emojiArray.filter{ uniqueCodes.contains([=10=]["UniqueCode"] as! Int)}
for item in filteredArray {
print(item["UniqueCode"] as! Int, item["Price"] as! Int / 2)
}
} catch {
print(error)
}
}
Swift 2 等价物(据我所知)是
if let url = NSBundle.mainBundle().urlForResource("Computer", withExtension: "plist") {
...
let data = try NSData(contentsOfURL:url)
let emojiArray = try NSPropertyListSerialization.propertyListFromData(data, options: [], format: nil) as! [[String:AnyObject]]
如果 Swift 2 语法错误,请注释掉这些行并使用代码完成重新键入它们。
所以我有一个名为 Computer 的 plist 文件,上面有 5 个项目。
每个项目都有这些键:
Type
Price
UniqueCode
例如
Item 0
Type: Desktop, Price 100, UniqueCode 321
Item 1
Type: Tablet, Price 150, UniqueCode 322
Item 2
Type: Laptop, Price 200, UniqueCode 323
Item 3
Type: Desktop, Price 210, UniqueCode 324
Item 4
Type: Laptop, Price 400, UniqueCode 325
然后我有一个class:
并且我输入了以下代码:
var emojiArray: NSArray?
if let path = NSBundle.mainBundle().pathForResource("Computer", ofType: "plist"){
emojiArray = NSArray(contentsOfFile: path)
}
我知道它不完整,但我只能做到这一点。我的主要objective是显示UniqueCode 321和324的电脑的价格,同时我也要把他们的价格减半
我该如何编码?
从你的 plist 描述来看,你似乎有一个字典数组。
看起来像这样的东西:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Type</key>
<string>Desktop</string>
<key>Price</key>
<integer>100</integer>
<key>Unique Code</key>
<integer>321</integer>
</dict>
<dict>
<key>Type</key>
<string>Laptop</string>
<key>Price</key>
<integer>400</integer>
<key>Unique Code</key>
<integer>235</integer>
</dict>
</array>
</plist>
因此,一旦加载了 emojiArray,就可以遍历数组并获取所需的值。
类似于:
if let path = Bundle.main.path(forResource: "Computer", ofType: "plist") {
if let emojiArray = NSArray(contentsOfFile: path) {
if let typedEmojiArray = emojiArray as? Array< Dictionary< String, AnyObject > > {
for eachEntry in typedEmojiArray {
if let uniqueCode = eachEntry["Unique Code"] as? NSNumber {
if let computerType = eachEntry["Type"] as? String {
print("computer type is \(computerType) and unique code is \(uniqueCode)")
}
if uniqueCode == 321 {
print("this is a value we wanted")
}
}
}
}
}
}
这是在 Swift 3、b.t.w 中完成的。
此外,像这样的 Plist 不一定是在文件中存储数据的最佳方式。如果这是我的应用程序,我可能会尝试使用 CoreData(在启动之间保留数据)。
首先,强烈建议您不要在 Swift 中使用 NSArray / NSDictionary
,因为 Foundation 集合类型缺少类型信息。反序列化 属性 列表的推荐 class 是 PropertyListSerialization
.
(Swift 3) 代码打印 UniqueCode
的值和 Price
的一半 UniqueCode
在 uniqueCodes
[= 中的所有项目18=]
let uniqueCodes = [321, 324]
if let url = Bundle.main.url(forResource:"Computer", withExtension: "plist") {
do {
let data = try Data(contentsOf:url)
let emojiArray = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [[String:Any]]
let filteredArray = emojiArray.filter{ uniqueCodes.contains([=10=]["UniqueCode"] as! Int)}
for item in filteredArray {
print(item["UniqueCode"] as! Int, item["Price"] as! Int / 2)
}
} catch {
print(error)
}
}
Swift 2 等价物(据我所知)是
if let url = NSBundle.mainBundle().urlForResource("Computer", withExtension: "plist") {
...
let data = try NSData(contentsOfURL:url)
let emojiArray = try NSPropertyListSerialization.propertyListFromData(data, options: [], format: nil) as! [[String:AnyObject]]
如果 Swift 2 语法错误,请注释掉这些行并使用代码完成重新键入它们。