gpt4 book ai didi

ios - 在 plist 字典中查找字符串

转载 作者:行者123 更新时间:2023-11-28 10:38:06 25 4
gpt4 key购买 nike

我试图在 plist 字典中找到一个字符串,但我不确定如何找到。我能得到一些帮助吗?

该代码包含两个 plist,一个包含客户列表,第二个包含列表或产品,我们将客户的数据填充到 ClientArray 的一个单元格中,但我还需要包含来自该客户的 ProductName ProductArray在同一个Cell中,匹配的key是productID。

enter image description hereplist 客户端数组

enter image description hereplist 产品数组

import UIKit

class TestViewController: UIViewController {
var ClientArray = [[String:Any]]()
var ProductArray = [[String:Any]]()

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

//path of plist file Array Client
let path1 = Bundle.main.path(forResource: "ClientList", ofType: "plist")
ClientArray = NSArray(contentsOfFile: path1!)! as! [Any] as! [[String : Any]]

//path of plist file Array Products
let path2 = Bundle.main.path(forResource: "ProductList", ofType: "plist")
ProductArray = NSArray(contentsOfFile: path2!)! as! [Any] as! [[String : Any]]
// Do any additional setup after loading the view, typically from a nib.
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as! TestTableViewCell

//fill out custom cell values
cell.testName.text = ClientArray[indexPath.row]["name"] as? String
cell.testNumber.text = ClientArray[indexPath.row]["number"] as? String


for product in ProductArray {
if let productName = product[ClientArray[indexPath.row]["productID"] as! String] {
cell.testProduct.text = productName["productName"] as? String
}
}

return cell
}
}

最佳答案

首先不要在 Swift 中使用 NSArrayNSDictionary。使用 native 类型。这避免了像 NSArray ... 这样奇怪的类型转换舞蹈! [任何]作为! [[字符串:任意]]

其次有一个类 PropertyListSerializationProperty List 转换为集合类型,反之亦然。

最后两个数组的正确类型是[[String:String]]。这避免了更多不必要的类型转换。

请遵守变量名以小写字母开头的命名规则。

var clientArray = [[String:String]]()
var productArray = [[String:String]]()

override func viewDidLoad() {
super.viewDidLoad()

//URL of plist file Array Client
let clientURL = Bundle.main.url(forResource: "ClientList", withExtension: "plist")!
let clientData = try! Data(contentsOf: clientURL)
clientArray = try! PropertyListSerialization.propertyList(from: clientData, format: nil) as! [[String:String]]

//URL of plist file Array Products
let productURL = Bundle.main.url(forResource: "ProductList", withExtension: "plist")!
let productData = try! Data(contentsOf: productURL)
productArray = try! PropertyListSerialization.propertyList(from: productData, format: nil) as! [[String:String]]
// Do any additional setup after loading the view, typically from a nib.
}

cellForRow 中使用 first 函数过滤产品名称。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as! TestTableViewCell

//fill out custom cell values
let client = clientArray[indexPath.row]
cell.testName.text = client["name"]
if let product = productArray.first{ $0["productID"]! == client["productID"]! } {
cell.testNumber.text = product["productName"]
}

return cell
}

一个更有效的解决方案是使用 PropertyListDecoder 将属性列表解码为结构

struct Client : Decodable {
let name, number, productID : String
}

struct Product : Decodable {
let productID, productName, productQty : String
}

...

var clients = [Client]()
var products = [Product]()

override func viewDidLoad() {
super.viewDidLoad()

//URL of plist file Array Client
let clientURL = Bundle.main.url(forResource: "ClientList", withExtension: "plist")!
let clientData = try! Data(contentsOf: clientURL)
clients = try! PropertyListDecoder().decode([Client].self, from: clientData)

//URL of plist file Array Products
let productURL = Bundle.main.url(forResource: "ProductList", withExtension: "plist")
let productData = try! Data(contentsOf: productURL)
products = try! PropertyListDecoder().decode([Product].self, from: productData)
}

...


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as! TestTableViewCell

//fill out custom cell values
let client = clients[indexPath.row]
cell.testName.text = client.name
if let product = products.first{ $0.productID == client.productID } {
cell.testNumber.text = product.productName
}

return cell
}

考虑将 CoreData 与数据模型的关系一起使用。它仍然更有效率。

关于ios - 在 plist 字典中查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52286786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com