Organizational Research By

Surprising Reserch Topic

nsurlconnection using ios swift

nsurlconnection using ios swift  using -'ios,swift,nsurlconnection,nsurlrequest'

I am trying to follow this tutorial and connect to a JSON api using Swift and NSURLConnection. I can see that it is hitting the url but the connectionDidFinishLoading does not seem to fire.

import UIKit

class Remote: NSObject {

    var host = "http://localhost:3000"
    var query = String()
    var data: NSMutableData = NSMutableData()

    func connect(query:NSString) {
        self.query = query
        var url = self.document()
        var conn = NSURLConnection(request: url, delegate: self, startImmediately: true)

    func endpoint() -> NSURL {
        var query = + self.query
        return NSURL(string: query)

    func document() -> NSURLRequest {
        return NSURLRequest( URL: self.endpoint() )

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        // Recieved a new request, clear out the data object = NSMutableData()

    func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
        // Append the recieved chunk of data to our data object

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        // Request complete, should now hold the resulting info
        // Convert the retrieved data in to an object through JSON deserialization
        var err: NSError
        var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(, options:    NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary



// Excecute the code

var remote = Remote()

At this point I am just trying to see the data that is returned. I would like to hook it into a view controller once I am sure this is working. Is there something wrong with doing it this way and that is causing an issue?

asked Oct 11, 2015 by mannar kande
0 votes

Related Hot Questions

2 Answers

0 votes
answered Oct 11, 2015 by vimaldas2005
0 votes
answered Oct 11, 2015 by sachin valanju