String concatenation problem with Swift

I am trying to use string concatenation to generate a url for a network call. However, I am getting intermittent results when trying to concatenate strings.

The attached image shows that it is combined with only "h"

. The relevant code has been added below.

Did I miss something?

Thank.

enter image description here

/* Constants */
private let baseURL    = "https://someurl.com/"
private let URLSuffix  = "anotherString"
private let typeItem   = "item/"

class HITNetworkCoordinator: NSObject {

    class var sharedInstance : HITNetworkCoordinator {
        return _HITNetworkCoordinatorInstance
    }

    func downloadItem (itemID: Int) {
        let taskURLString = baseURL + typeItem + String(itemID) + URLSuffix
        let taskURL = NSURL.URLWithString(taskURLString)
        let task = NSURLSession.sharedSession().dataTaskWithURL(taskURL, completionHandler: { (data, response, error) -> Void in
            var responseDict : NSDictionary? = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? NSDictionary
            println(responseDict)
        })
        task.resume()
    }

      

+3


source to share


2 answers


Try this: let taskURLString = "(baseURL) (typeItem) + String (itemID) + (URLSuffix)" and see what happens



0


source


String concatenation seems more fragile as you increase the number of concat (+) statements in the expression. I gave up a while ago and started using evaluators instead. For example. let taskURLString = "\ (baseURL) \ (typeItem) \ (itemID) \ (URLSuffix)"



0


source







All Articles