Найти строку в списке словаря - PullRequest
0 голосов
/ 12 сентября 2018

Я пытаюсь найти строку в словаре plist Dictionary, но я не знаю как.Могу ли я получить некоторую помощь, пожалуйста?

Код содержит два списка, один со списком клиентов, а второй со списком или продуктами, мы заполняем данные клиента в ячейке из 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 голосов
/ 12 сентября 2018

Прежде всего, не используйте NSArray и NSDictionary в Swift.Используйте нативные типы.Это позволяет избежать таких странных танцев, как 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 фильтруйте имя продукта с помощью firstfunction.

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 со связями для модели данных.Это еще более эффективно.

0 голосов
/ 12 сентября 2018

Во-первых, я рекомендую использовать правильный тип данных здесь.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
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...