2

I am trying to Create Block in Swift.

I have created class and declared block like this

typealias JSONParserBlock = (dict:NSDictionary?,error:NSError?) -> Void

Variable declaration:

var block: JSONParserBlock!

and Function:

func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void
{}

But my problem is when I call this function from another class, this function does not execute

Function call is like this:

var objJsonParser:JsonParser?

objJsonParser?.Getdata(str as String, RequsetedParameter:Dictionary, BLOCK:
    { (dict, error) in
        print("success")
})

Custom class code

import UIKit

class JsonParser: NSObject,NSURLConnectionDelegate
{
    typealias JSONParserBlock = (dict:NSDictionary?,error:NSError?) -> Void

    var block: JSONParserBlock!
    var Data:NSMutableData?=nil
    var delegate:JsonParserDelegate?
    var DICT:NSDictionary!
    var error: NSErrorPointer=nil
    func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void
    {
        self.block=BLOCK;
        Data = NSMutableData()
        let urlPath: String = "http://192.168.13.2/freshbakala/admin/json/GetCategory.php"
        let url: NSURL = NSURL(string: urlPath)!
        let request: NSURLRequest = NSURLRequest(URL: url)
        let connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)!
        connection.start()
    }

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
    {
        self.Data = NSMutableData()
    }
    func connection(connection: NSURLConnection!, didReceiveData data: NSData!)
    {
        self.Data?.appendData(data)
    }
    func connectionDidFinishLoading(connection: NSURLConnection!)
    {
        let error:NSError
        let jsonresult:NSDictionary
        print("Response is received")
        do
        {
            jsonresult = try NSJSONSerialization.JSONObjectWithData(self.Data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
            print(jsonresult)
            block(dict: jsonresult,error: nil)

        }
        catch
        {

        }

    }
}
1
  • Could you show whole code of this two classes ? Commented Apr 25, 2016 at 13:04

2 Answers 2

1

Another problem, in addition to the issue pointed out by Mrwerdo is that this code:

var objJsonParser:JsonParser?

objJsonParser?.Getdata(str as String, RequsetedParameter:Dictionary, BLOCK: {
    (dict, error) in
    print("success")
})

doesn't actually do anything and won't complain because objJsonParser is nil unless you assign something to it and the ? in the invocation means that GetData will do nothing because the object is nil.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to call the closure in the GetData function. Something like this should do:

func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void
{
  let dict: NSDictionary? = nil    // set these to something meaningful
  let error: NSError? = nil
  BLOCK(dict: dict, error: error)  // call the closure
}

1 Comment

Yes, BLOCK is just a parameter to the function. The function body is empty, so the block does not get executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.