Selvakumar
Selvakumar

Reputation: 45

SWIFT URL String build

How do I build the URL as below:

/sap/opu/odata/SAP/ZTM_FOR_MOBILITY_SRV/BusinessPartnerSet('BP-CR-EM01')/?$format=json

I want specifically the part - BusinessPartnerSet('BP-CR-EM01').

BP-CR-EM01 value is dynamic value.

let newUrl = url  + "'\(businessPartners[myIndex].partnerId)'"

url has the fixed URL and the + operator followed by dynamic value. Please help me with your suggestions.

Upvotes: 2

Views: 1129

Answers (1)

vadian
vadian

Reputation: 285082

I recommend to use URLComponents

var components = URLComponents(string: "http://mycompany.com")!
components.path = "/sap/opu/odata/SAP/ZTM_FOR_MOBILITY_SRV/BusinessPartnerSet('\(businessPartners[myIndex].partnerId)')/"
components.queryItems = [URLQueryItem(name: "$format", value:"json")]
if let url = components.url {
    print(url)
    // or if you need the string
    print(url.absoluteString)
}

Upvotes: 4

Related Questions