在 plist 字典中查找字符串
Find a string inside a plist Dictionary
我正在尝试在 plist 字典中查找字符串,但不确定如何查找。我能得到一些帮助吗?
该代码包含两个 plist,一个包含客户列表,第二个包含列表或产品,我们将客户的数据填充到 ClientArray 的一个单元格中,但我还需要为此包含 ProductName client 来自同一个 Cell 中的 ProductArray,匹配的 key 是 productID。
plist 客户端数组
plist ProductArray
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
}
}
首先,我建议在这里使用正确的数据类型。
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
}
}
}
首先不要在Swift中使用NSArray
和NSDictionary
。使用本机类型。这避免了像 NSArray ... as! [Any] as! [[String : Any]]
.
这样奇怪的演员舞蹈
其次是 class 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{ [=11=]["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{ [=12=].productID == client.productID } {
cell.testNumber.text = product.productName
}
return cell
}
考虑将 CoreData 与数据模型的关系一起使用。效率还是比较高的。
我正在尝试在 plist 字典中查找字符串,但不确定如何查找。我能得到一些帮助吗?
该代码包含两个 plist,一个包含客户列表,第二个包含列表或产品,我们将客户的数据填充到 ClientArray 的一个单元格中,但我还需要为此包含 ProductName client 来自同一个 Cell 中的 ProductArray,匹配的 key 是 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
}
}
首先,我建议在这里使用正确的数据类型。 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
}
}
}
首先不要在Swift中使用NSArray
和NSDictionary
。使用本机类型。这避免了像 NSArray ... as! [Any] as! [[String : Any]]
.
其次是 class 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{ [=11=]["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{ [=12=].productID == client.productID } {
cell.testNumber.text = product.productName
}
return cell
}
考虑将 CoreData 与数据模型的关系一起使用。效率还是比较高的。