首页 > 解决方案 > 如果值为 nil,则 Swfit XML Parse 不起作用

问题描述

您好有问题,如果 xml 值为 nil,它将不会在我的列表视图上显示我的任何其他 xml 值。

有时 hold_reason 有一个值,有时它没有,但是当我尝试将数据输出到我的列表视图时,它不会提取任何 xml 值,除非我删除代码以获取 hold_reason 我如何制作它所以如果没有价值设置默认值“”

<?xml version="1.0"?>
<order_list>
<order>
<order_id>20-071523937</order_id>
<status>OPEN</status>
<est_delivery_date>07/21/2020</est_delivery_date>
<service>USPSPRI</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49553</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488225169494</other_ref_id1>
<other_ref_id2>#49553</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071523950</order_id>
<status>OPEN</status>
<est_delivery_date>07/21/2020</est_delivery_date>
<service>USPSPRI</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49557</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488323309654</other_ref_id1>
<other_ref_id2>#49557</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071423594</order_id>
<status>OPEN</status>
<est_delivery_date>07/24/2020</est_delivery_date>
<service>GND</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49487</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2486643621974</other_ref_id1>
<other_ref_id2>#49487</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071523953</order_id>
<status>OPEN</status>
<est_delivery_date>07/21/2020</est_delivery_date>
<service>USPSPRI</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49559</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488405065814</other_ref_id1>
<other_ref_id2>#49559</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071523939</order_id>
<status>OPEN</status>
<est_delivery_date>07/24/2020</est_delivery_date>
<service>GND</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49554</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488246501462</other_ref_id1>
<other_ref_id2>#49554</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071523956</order_id>
<status>OPEN</status>
<est_delivery_date>07/21/2020</est_delivery_date>
<service>USPSPRI</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49560</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488433508438</other_ref_id1>
<other_ref_id2>#49560</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071423319</order_id>
<status>OPEN</status>
<est_delivery_date>07/21/2020</est_delivery_date>
<service>USPSPRI</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49466</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2486222782550</other_ref_id1>
<other_ref_id2>#49466</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071523944</order_id>
<status>OPEN</status>
<est_delivery_date>07/24/2020</est_delivery_date>
<service>GND</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49556</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488303943766</other_ref_id1>
<other_ref_id2>#49556</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<order_id>20-071523945</order_id>
<status>OPEN</status>
<est_delivery_date>07/24/2020</est_delivery_date>
<service>GND</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49555</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488293097558</other_ref_id1>
<other_ref_id2>#49555</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
</order>
<order>
<client_code>Client Name</client_code>
<order_id>20-071523952</order_id>
<status>OPEN</status>
<est_delivery_date>07/21/2020</est_delivery_date>
<service>USPSPRI</service>
<ship_bill_option>0</ship_bill_option>
<carrier_code></carrier_code>
<carrier_service_code></carrier_service_code>
<ship_carrier_account></ship_carrier_account>
<bill_customs_opt>2</bill_customs_opt>
<web_store_id>2190</web_store_id>
<cust_ref_id>#49558</cust_ref_id>
<po_number></po_number>
<other_ref_id1>2488345133142</other_ref_id1>
<other_ref_id2>#49558</other_ref_id2>
<other_ref_id3></other_ref_id3>
<request_ship_date>07/15/2020</request_ship_date>
<needed_by_date></needed_by_date>
**<hold_reason></hold_reason>**
</order>
</order_list>
if let data = data {
                let xml = XML.parse(data)
                //print("XML Values:",xml)
                let orders = xml["order_list"]["order"]
                //print("XML Values Orders:",orders)
                    
                let orderItems = orders.compactMap { (indexer: XML.Accessor) -> OrderItem? in
                if
                let client_name = indexer["client_code"].element?.text,
                let order_id = indexer["order_id"].element?.text,
                let status = indexer["status"].element?.text,
                let cust_ref_id = indexer["cust_ref_id"].element?.text,
                let hold_reason = indexer["hold_reason"].element?.text,
                let service = indexer["service"].element?.text {
                    return OrderItem(client_name: client_name, order_id: order_id, status: status, cust_ref_id: cust_ref_id, service: service, hold_reason: hold_reason)
                }
                return nil
                }
                
                //print("Orders Object :", orderItems);
                
                completion(orderItems)
                }

标签: swiftxcode

解决方案


推荐阅读