我试图在plist词典中找到一个字符串,但我不确定如何。我能得到一些帮助吗?
代码包含两个plists,一个包含客户端列表,第二个包含列表或Products,我们在ClientArray的单元格中填充客户端的数据,但我还需要从ProductArray中包含该客户端的ProductName。同一个Cell,匹配的密钥是productID。
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中使用NSArray
和NSDictionary
。使用本机类型。这避免了奇怪的演员舞蹈,如NSArray ... as! [Any] as! [[String : Any]]
。
其次,有一个类PropertyListSerialization
将Property 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
将Property List解码为结构
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
}
考虑使用核心数据与数据模型的关系。它仍然更有效率。
首先,我建议在这里使用正确的数据类型。 plist可以是字典
例如:
if let path = Bundle.main.path(forResource: "ClientList", ofType: "plist"), let clientDict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {
}
然后你将有2个字典,你需要访问最大文件的每个项目的productID
(一个循环)并迭代最小文件的项目(n个循环),以找到相同的productID
并匹配数据。
let clients = ["item0": ["productId": "10002"], "item1": ["productId": "10005"]]
let products = ["item0": ["productId": "10002"], "item1": ["productId": "10005"], "item2": ["productId": "10004"]]
let specialKey = "productId"
for product in products {
for client in clients {
if client.value[specialKey] == product.value[specialKey] {
print("Product found!")
break
}
}
}