在plist字典中查找字符串

时间:2018-09-12 02:49:34

标签: ios swift plist

我正在尝试在plist词典中找到一个字符串,但不确定如何。我可以帮忙吗?

该代码包含两个复制者,一个包含“客户端”列表,第二个包含“列表”或“产品”,我们正在从ClientArray的单元格中填充该客户端的数据,但我还需要包括该客户端的ProductName同一单元格中的ProductArray,匹配键为productID。

enter image description here plist ClientArray

enter image description here 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
    }
}

2 个答案:

答案 0 :(得分:2)

首先在Swift中不要使用NSArrayNSDictionary。使用本机类型。这样可以避免像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

将属性列表解码为结构
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与数据模型的关系一起使用。效率更高。

答案 1 :(得分:1)

首先,我建议在此处使用正确的数据类型。 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
        }
    }
}