swift contsruct xml请求体的请求

时间:2016-09-04 17:57:37

标签: ios xml swift

嗨我即将在swift中创建自定义请求

我的请求正文有问题。

我的身体在xml:

 package uz.salvadore.app;

 import uz.salvadore.rest.MessageRestService;

 import javax.ws.rs.core.Application;
 import java.util.HashSet;
 import java.util.Set;

 public class MessageApplication extends Application {

    private Set<Object> singletons = new HashSet<Object>();

    public MessageApplication() {
        singletons.add(new MessageRestService());
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }

 }

如何将其转换为字符串,然后将数据转换为请求的数据?

我试过

<ItemTrackingDetailsRequest xmlns="http://singpost.com/paw/ns">
<ItemTrackingNumbers>
<TrackingNumber>SMT0000000628</TrackingNumber>
</ItemTrackingNumbers>
</ItemTrackingDetailsRequest>

但是在params中有错误,因为我错误地构造了body String。有什么办法从上面的xml构造String?任何帮助都非常感激。感谢

1 个答案:

答案 0 :(得分:3)

Swift不支持多行字符串文字,我也没有看到任何提案。如果您提出了很多请求,最好将请求包装在为您构建XML的结构中:

struct ItemTrackingRequest {
    var trackingNumbers: [String]

    init(trackingNumbers: String...) {
        self.trackingNumbers = trackingNumbers
    }

    func xmlString() -> String {
        var xml = "<?xml version=\"1.0\"?>"
        xml += "<ItemTrackingDetailsRequest xmlns=\"http://singpost.com/paw/ns\">"
        xml += "<ItemTrackingNumbers>"

        for number in self.trackingNumbers {
            xml += "<TrackingNumber>\(number)</TrackingNumber>"
        }

        xml += "</ItemTrackingNumbers>"
        xml += "</ItemTrackingDetailsRequest>"

        return xml
    }
}

let params = ItemTrackingRequest(trackingNumbers: "SMT0000000628").xmlString()

如果您的请求变得复杂(比较复杂),请使用Core Data为请求建模并为其生成XML。